嘗試這樣,我希望它會幫助你。
// OBJC
UIImage *img_table =[UIImage imageNamed:@"right.png"];
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, img_table.size.width, img_table.size.height);
[button setImage:img_table forState: UIControlStateNormal];
[button setImage:img_table forState: UIControlStateSelected];
[button addTarget: self action:@selector(rightBtnClicked:) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
barButton.tintColor=[UIColor whiteColor];
self.navigationItem.rightBarButtonItem = barButton;
//斯威夫特
let img_table = UIImage(named: "right.png")
let button = UIButton.init(type: UIButtonType.custom)
button.frame = CGRect.init(x: 0, y: 0, width: (img_table?.size.width)!, height: (img_table?.size.height)!)
button.setImage(img_table, for: UIControlState.normal)
button.setImage(img_table, for: UIControlState.selected)
button.addTarget(self, action: @selector(rightBtnClicked:)), for: UIControlEvents.touchUpInside)
let barButton = UIBarButtonItem.init(customView: button)
barButton.tintColor=[UIColor whiteColor];
self.navigationItem.rightBarButtonItem = barButton
沒有你試過這種https://stackoverflow.com/a/5419719/4955401? –
您提供的鏈接不顯示導航欄中的按鈕。 @ParvBhasker –
'rightBtnClicked'發生了什麼? – Codus