アプリでNavigationControllerのツールバーを使用しようとしています。このツールバーのtoolbarItemsは、表示されるViewControllerに応じて変更されると想定されています。これは非常に基本的なことです。
私がやろうとしているのは、UIBarButtonItemの「initWithCustomView:」メソッドを使用してツールバーにカスタムボタンを追加することです。ただし、ボタンはツールバーに表示されません。しかし、「initWithTitle:」または「initWithBarButtonSystemItem:」メソッドを使用してUIBarButtonItemを作成すると、ボタンが表示されます。たとえば、次のコードを見てください。
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"edit" style:UIBarButtonItemStylePlain target:self action:nil];
NSArray *array = [[NSArray alloc] initWithObjects:item1, item2, nil];
[self setToolbarItems:array];
これを行うと、ツールバーにボタンが表示されます。しかし、私が次のことを行うとしたら:
UIButton* replyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[replyBtn setImage:[UIImage imageNamed:@"Reply_Message.png"] forState:UIControlStateNormal];
[replyBtn addTarget:self action:@selector(replyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
replyBtn.frame = CGRectMake(10, 0, 40, 40);
UIBarButtonItem *replyButton = [[UIBarButtonItem alloc] initWithCustomView:replyBtn];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"edit" style:UIBarButtonItemStylePlain target:self action:nil];
NSArray *array = [[NSArray alloc] initWithObjects:item1, replyButton, item2, nil];
[self setToolbarItems:array];
このコードでは、item1とitem2のみがツールバーに表示されます。replyButtonはツールバーに表示されません。ボタンがあるはずの場所に空白があります。
これと同じコードを、NavigationControllerのツールバーの代わりに作成した通常のツールバーで使用すると、ボタンが表示されます。私は、Appleのメールアプリケーションと同じ感覚を持たせるために、アプリ全体で1つのツールバーを使用しようとしています。「initWithCustomView:」メソッドを使用する必要がある理由は、アイコンの1つが色付きであり、これが通常のツールバーに色付きで表示される唯一の方法であるためです。今、私はアップルのドキュメントを調べましたが、「initWithCustomView:」メソッドを呼び出せなかった(または見つからなかった)理由については何も言及されていません。
私が正しい方向を示すのを助けるために、誰かがこのトピックに光を当ててください。よろしくお願いします。