I can't set a custom left bar button item in my app's navigation bar. Here's what I did:
let profileImage = UIImage(named: "researcher_icon_selected")
let button = UIBarButtonItem(image: profileImage, style: .plain, target: self, action: #selector(self.viewDidLoad))
self.navigationItem.setLeftBarButton(button, animated: true)
I put this code in my viewDidLoad of a subclassed UINavigationController called "ExpertsNavigationViewController." But nothing is showing up in the nav bar!
Here are a few debugging experiments I tried:
1) I printed the left bar button item picture dimensions and indeed it was the correct dimensions from the custom image I set. So the instance is there, it's just not showing up in the view.
2) I tried this in a completely new XCode project and the custom bar button item appeared!
3) Because of this I tried to make a completely new custom navigation controller in my current project, made this the initial view controller, inserted the above code in its viewDidLoad, but again, nothing showed up!
4) I thought maybe I did something in the App Delegate, or the root view of the navigation controller to change the navigation bar. But nothing there at all!
Any ideas? I would appreciate any hints as to why the bur button item would not appear. Thanks.
The problem is that you're configuring navigation controller's navigationItem
, but you need to configure navigationItem
of root view controller from navigation controller. Move your code to root view controller's viewDidLoad()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With