Search code examples
iosswiftuinavigationbaruibarbuttonitemuinavigationitem

How to add a standard info button to a navigation bar in iOS?


I'd like to add a right bar button with the system's info icon to a navigation bar. But I see that UIBarButtonSystemItem does not provide such type of button. On the other hand, so I tried with a UIButton of type UIButtonType.infoLight, but I am not allowed to assign it to navigationItem.rightBarButtonItems.

Is there any way to do this?


Solution

  • this can be achieved using UIButton object of type .infoLight

    let infoButton = UIButton(type: .infoLight)
    infoButton.addTarget(self, action: #selector(infoButtonTapped), forControlEvents: .TouchUpInside)
    let barButton = UIBarButtonItem(customView: infoButton)
    self.navigationItem.rightBarButtonItem = barButton
    

    alternatively:

    by adding infoImage to your Asset catalog and create barButtonItem using below code

    let infoButton = UIBarButtonItem(image: UIImage(named: "infoImage"), style: .Plain, target: self, action: #selector(ViewController.infoButtonTapped))
    self.navigationItem.rightBarButtonItem = infoButton