0

I have a ViewController called SourceViewController that is embedded in a NavigationController.

SourceViewController segues to DestinationViewController upon UITableViewCell selection.

I want to hide the navigation bar on SourceViewController, but display it on DestinationViewController in order to show the Back button.

So, in SourceViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.navigationBar.hidden = true
}

And in DestinationViewController: override func viewDidLoad() { super.viewDidLoad()

    self.navigationController?.navigationBar.hidden = false
}

However, when I tap "Back" in DestinationViewController to return to SourceViewController, the navigationBar reappears in SourceViewController

The next 'obvious' step would be to set navigationBar.hidden = false in viewDidAppear in SourceViewController, however this smells for many reasons: mainly DRYness but also when returning to SourceViewController, there is a delay in hiding the navigationBar, and it is visible for a split second.

How do I solve this problem?

Eric Johnson
  • 1,087
  • 1
  • 11
  • 16
  • Could you please check answer of this question ?? http://stackoverflow.com/questions/845583/iphone-hide-navigation-bar-only-on-first-page – Alvin Varghese Nov 23 '15 at 18:42

2 Answers2

0

Check ViewController lifecycle Looking to understand the iOS UIViewController lifecycle . When you start the program viewDidLoad is called and everything is ok, but when you go back from detailController, viewDidLoad is not called, just change this line (self.navigationController?.navigationBar.hidden = true) in viewWillApear and everything must be ok.

Community
  • 1
  • 1
Damyan Todorov
  • 536
  • 1
  • 6
  • 17
0

I think this will work, hiding the navigation bar. before appearing/disappearing the view.

override func viewWillAppear(animated: Bool) {
    navigationController?.navigationBarHidden = true
    super.viewWillAppear(animated)
}


override func viewWillDisappear(animated: Bool) {
    navigationController?.navigationBarHidden = true
    super.viewWillDisappear(animated)
}
Alvin Varghese
  • 842
  • 10
  • 33