Search code examples
iosswiftuinavigationcontrolleruinavigationbar

Add custom button to navigation controller


So I want to add a custom settings button with a custom background image in my navigation controller. Currently I tried doing this:

        func viewDidLoad() {
            super.viewDidLoad()
            
            /// Settings button:
            let settingsButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
            settingsButton.setImage(UIImage(named: "Configuration Icon"), for: .normal)
            settingsButton.addTarget(self, action: #selector(configurationTapped), for: .touchUpInside)
            self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: settingsButton)
        }

When I tap this button, configurationTapped gets triggered, but the problem is that the button doesn't blink as it normally does when I put it outside the navigation controller..

How can I solve this??
Should I create a custom animation every time the action gets called, or am I missing something here?


Solution

  • let yourButton = UIBarButtonItem(image: yourImage, style: .done, target: self, action: #selector(yourSelector))
    self.navigationItem.rightBarButtonItem = yourButton