How to have a custom navigation controller supply the same custom navBar buttons to each storyboard

Question

What I want is to use custom buttons instead of the back button and title inside the nav bar of a navigation controller. I created a custom navigation controller. Inside the custom nav controller(inside of the viewdidload function), I have this:

let leftButtonItem = UIBarButtonItem.init(
     title: "My Connections",
     style: .done,
     target: self,
     action: #selector(goToPartners)
     )

   let middleButtonItem = UIBarButtonItem.init(
     title: "Home",
     style: .done,
     target: self,
     action: #selector(goHome)
     )

self.navigationItem.setLeftBarButtonItems([leftButtonItem, middleButtonItem], animated: false)

I have no errors, but nothing shows up in my nav bar except for the title(which is automatically inherited from that storyboards title)


Show source
| xcode   | swift   | ios   | uinavigationcontroller   | uinavigationitem   2017-08-17 00:08 3 Answers

Answers to How to have a custom navigation controller supply the same custom navBar buttons to each storyboard ( 3 )

  1. 2017-08-17 00:08

    Overriding viewDidLoad of the navigation controller isn't going to help you as it will only execute when the navigation contoller is first loaded. As each view controller is pushed, the button items will be updated.

    I suggest that you implement a UINavigationControllerDelegate and use that to update the navigationItem in the navigationController(_:willShow:animated:) method

  2. 2017-08-17 00:08

    The setLeftBarButtonItems will not work in this case. This is because setLeftBarButtonItems is used to execute transition to the bar button items.

    Just replace it with leftBarButtonItems as given below in ViewDidLoad of your View Controller:

    self.navigationItem.leftBarButtonItems = [leftButtonItem, middleButtonItem]
    

    That should solve your problem.

  3. 2017-08-17 19:08

    I fixed it, I ended up having to make a custom uiviewcontroller class, and used setleftbarbutton items and setrightbarbutton inside the viewdidload function, then just inheriting the class into all my viewcontrollers.

Leave a reply to - How to have a custom navigation controller supply the same custom navBar buttons to each storyboard

◀ Go back