Search code examples
swiftuibuttonuibarbuttonitem

How to use info button on a UIBarButtonItem?


How can I use the info light on a UIBarButtonItem?

I don't want to use a custom image because the result is not very nice.

I would like to use the Apple "info" button on a UIBarButtonItem using Swift.


Solution

  • There is no direct way to create such a bar button using the UIBarButtonItem APIs.

    You can although use a custom view configured with a .InfoLight UIButton, as suggested here:

    // Create the info button
    let infoButton = UIButton(type: .infoLight)
    
    // You will need to configure the target action for the button itself, not the bar button itemr
    infoButton.addTarget(self, action: #selector(getInfoAction), for: .touchUpInside)
    
    // Create a bar button item using the info button as its custom view
    let infoBarButtonItem = UIBarButtonItem(customView: infoButton)
    
    // Use it as required
    navigationItem.rightBarButtonItem = infoBarButtonItem
    

    Feel free to leave a comment if you need any more help.