Eric Johnson Eric Johnson - 5 months ago 11
iOS Question

Set NavigationBar for Destination ViewController Only

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?

Answer

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)
}