Reputation: 5532
I am trying to have 2 buttons on the right of a UINavigationBar
. Below is the source code. No error but there is no button either. It is a UIViewController
, not a UINavigationViewController
let buttonEdit: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
buttonEdit.frame = CGRectMake(0, 0, 40, 40)
buttonEdit.setImage(UIImage(named:"me44.png"), forState: UIControlState.Normal)
buttonEdit.addTarget(self, action: "rightNavItemEditClick:", forControlEvents: UIControlEvents.TouchUpInside)
var rightBarButtonItemEdit: UIBarButtonItem = UIBarButtonItem(customView: buttonEdit)
let buttonDelete: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
buttonDelete.frame = CGRectMake(0, 0, 40, 40)
buttonDelete.setImage(UIImage(named:"me44.png"), forState: UIControlState.Normal)
buttonDelete.addTarget(self, action: "rightNavItemDeleteClick:", forControlEvents: UIControlEvents.TouchUpInside)
var rightBarButtonItemDelete: UIBarButtonItem = UIBarButtonItem(customView: buttonDelete)
// add multiple right bar button items
self.navigationController?.navigationItem.setRightBarButtonItems([rightBarButtonItemDelete, rightBarButtonItemEdit] as [AnyObject], animated: true)
//I also tried code below no luck either
self.navigationItem.setRightBarButtonItems([rightBarButtonItemDelete, rightBarButtonItemEdit] as [AnyObject], animated: true)
Upvotes: 2
Views: 4048
Reputation: 156
Despite all indicated previously, it's possible that you don't see the added button to navigation bar. For Swift 3, try this in the AppDelegate class (pay attention to topItem):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
window?.backgroundColor = UIColor.white
let navigationVC = UINavigationController(rootViewController: UIViewController())
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTodo))
navigationVC.navigationBar.topItem?.rightBarButtonItem = addButton
window?.rootViewController = navigationVC
window?.makeKeyAndVisible()
return true
}
Upvotes: 1
Reputation: 535915
This code is just wrong:
self.navigationController?.navigationItem.setRightBarButtonItems(
[rightBarButtonItemDelete, rightBarButtonItemEdit] as [AnyObject], animated: true)
You do not set the navigation controller's navigationItem
; you set your navigationItem
. Also, the [AnyObject]
thing is just unnecessary. So:
self.navigationItem.setRightBarButtonItems(
[rightBarButtonItemDelete, rightBarButtonItemEdit], animated: true)
However, note that that works only if your view controller is the child of a UINavigationController. Setting a view controller's navigationItem
populates a navigation bar automatically only in that situation. If you are not in that situation — i.e., if you just have a "loose" navigation bar in your interface — you need to populate your navigation bar manually (by setting its navigationItem
).
(Also, note that if you have no "me44.png"
image, it could be that your code is working but you just aren't seeing anything.)
Upvotes: 8