カスタム ナビゲーション バー ボタン項目を実装しようとしています。ユーザーが項目をクリックして関数が呼び出されると、セレクターがあります。バー項目を設定するUIBarButtonItem
と問題なく動作しますが、境界線と適切なサイズのないボタンにカスタム イメージを使用する必要があります。
だからviewdidloadで私は電話する
- (void)viewDidLoad
{
[super viewDidLoad];
//gear button on navigation Bar
UIImage* imageback2 = [UIImage imageNamed:@"ICON - Gear-BarstyleItem@2x.png"];
CGRect frameimgback2 = CGRectMake(0, 0, 40, 40);
UIButton *backButton2 = [[UIButton alloc] initWithFrame:frameimgback2];
[backButton2 setBackgroundImage:imageback2 forState:UIControlStateNormal];
[backButton2 addTarget:self
action:@selector(setColorButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:backButton2];
UIBarButtonItem *btn3 =[[UIBarButtonItem alloc] initWithImage:imageback2 style:UIBarButtonItemStylePlain target:self action:@selector(setColorButtonTapped:)];
self.navigationItem.rightBarButtonItem = btn2;
}
#pragma mark Callbacks
- (IBAction)setColorButtonTapped:(id)sender{
if (_colorPicker == nil) {
self.colorPicker = [[ColorPickerController alloc] initWithStyle:UITableViewStylePlain];
_colorPicker.delegate = self;
self.colorPickerPopover = [[UIPopoverController alloc] initWithContentViewController:_colorPicker];
}
[self.colorPickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
これは私に与えます:
-[UIButton view]: unrecognized selector sent to instance 0x9466620
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton view]: unrecognized selector sent to instance 0x9466620'
私が設定した場合
self.navigationItem.rightBarButtonItem = btn2;
に
self.navigationItem.rightBarButtonItem = btn3;
正常に動作しますが、ボタンには境界線と背景色があり、画像のサイズが小さくなっています
では、なぜエラーが発生し、このボタンをカスタムボタンにするにはどうすればよいでしょうか?
編集:
次のようなvoid関数を追加すると
- (void)gearTapped{
[self setColorButtonTapped:self];
}
ボタンでセレクターを変更してもエラーは発生しませんが、ポップアップが画面の中央に1行だけ表示されます
[backButton2 addTarget:self
action:@selector(gearTapped)
forControlEvents:UIControlEventTouchUpInside];
通常はこのように動作するはずですが、カスタムボタンを使用します