Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set left bar button item in navigation bar?

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.

like image 709
Tariq Avatar asked Dec 28 '16 15:12

Tariq


1 Answers

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().

like image 83
Igor Bidiniuc Avatar answered Oct 20 '22 19:10

Igor Bidiniuc