Reputation: 1127
Suppose I have two views. If I'm in the first view and I perform a push segue to the second view. Then, I want to execute some code if the user presses the back button on the navigation bar or swipes right from the left side of the screen, is this possible?
I will tell you exactly what I want to do to see if there is a better way to do this. Pretty much my first view does NOT have a navigation bar. The second view however, does. When going from the first view to the second view there are no problems, as in the animation the navigation bar shows up.
However, when I go back to the first view from the second view, the navigation bar stays on the screen until it is completely back from the second view.
Thanks for your help!
Upvotes: 1
Views: 41
Reputation: 1127
So the way I fixed this was with the following method:
override func viewWillDisappear(_ animated: Bool)
I was simply unaware of this method.
Upvotes: 1
Reputation: 36620
The following code may be able to accomplish what you need.
This is triggered by a change in the navigation stack, and if the parent is nil, that indicates that the view is being dismissed.
In that check, you would just hide the navigation controller.
The following code is for Swift 3.0, and can be modified for Swift 2.2 fairly easily if need be.
override func willMove(toParentViewController parent: UIViewController?) {
super.willMove(toParentViewController: parent)
if parent == nil {
navigationController?.navigationBar.isHidden = true
}
}
Upvotes: 1