1

I am trying to add Home button to navigation controller. Therefore I created below class and sub classed my navigation controller. My button appears on my first view. When I navigate to other view(table view in my picture), added button disappears. I am using segues to push to another view.

class ThemedNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()


        var home = UIBarButtonItem(image: UIImage(named: "home"), style: UIBarButtonItemStyle.Plain, target: self, action: "doneAction")
        navigationBar.topItem?.rightBarButtonItem = home

        navigationBar.barTintColor = anaRenk
        navigationBar.barStyle = .Black
        navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: sansSerifName, size: 17)!]
        UIBarButtonItem.appearance().setTitleTextAttributes(
        [NSFontAttributeName: UIFont(name: sansSerifName, size: 17)!],
        forState: .Normal)
    }

    func doneAction() { // [6]
        self.navigationController?.popToRootViewControllerAnimated(true)

    }
}

enter image description here

Before my mainViewController didn't have navigation controller. Instead each button was pushing new viewcontrollers which had separate navigation controllers and my code was working. I will appreciate if you can tell me how can I fix this issue.

Meanteacher
  • 2,031
  • 3
  • 17
  • 48
  • This might help: http://stackoverflow.com/questions/6389094/adding-same-button-to-all-view-controllers-in-uinavigationcontroller?rq=1 – Masa Feb 07 '15 at 17:13

2 Answers2

0

You have to add the buttons in each controller as the nav bar updates for each controller when pushed on. hence why the back button label changes.

So put the same code in each controller you want a home button for.

I would not put any of the code in the Nav controller itself. Start with the first controller you want to pop home from.

Easier to drag a bar button item in using storyboard, then create an action for it which pops to root.

Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
  • Thanks. However is there any easy way to use some class etc so that I don't add that home button over and over again. – Meanteacher Feb 07 '15 at 17:52
  • You could create a sub class for each controller type you use which has the code in viewDidLoad. Then sub class that again for your specific controller class. when you call [super viewDidLoad] it will add the home button. – Rory McKinnel Feb 07 '15 at 18:00
0

I think I made it. It works however I am not sure if it is the best method. I will appreciate if you comment for any possible memory leak or crash issue. Thanks.

class ThemedNavigationController: UINavigationController {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    var home:UIBarButtonItem!
    override func viewDidLoad() {
        super.viewDidLoad()  

        home = UIBarButtonItem(image: UIImage(named: "home"), style: UIBarButtonItemStyle.Plain, target: self, action: "doneAction")

        navigationBar.barTintColor = anaRenk
        navigationBar.barStyle = .Black
        navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: sansSerifName, size: 17)!]
        UIBarButtonItem.appearance().setTitleTextAttributes(
        [NSFontAttributeName: UIFont(name: sansSerifName, size: 17)!],
        forState: .Normal)
    }

    override func pushViewController(viewController: UIViewController, animated: Bool) {

    var exbutton = viewController.navigationItem.rightBarButtonItem?
    if  exbutton == nil {
        viewController.navigationItem.rightBarButtonItem = home
    }
    else {
       viewController.navigationItem.rightBarButtonItems?.insert(home, atIndex: 0)

    }

        super.pushViewController(viewController, animated:animated)
    }

    func doneAction() { // [6]
        popToRootViewControllerAnimated(true)
    }
}
Meanteacher
  • 2,031
  • 3
  • 17
  • 48