Mzoch
Mzoch

Reputation: 133

how to override viewWillDisappear in Swift3

I'm Trying to hide the first navigation bar and show all the others, so I used:

override func viewWillAppear(_ animated: Bool) {
    // Hide the navigation bar on the this view controller
    self.navigationController?.setNavigationBarHidden(true, animated: true)
}

override func viewWillDisappear(_ animated: Bool) {
    // Show the navigation bar on other view controllers
    self.navigationController?.setNavigationBarHidden(false, animated: true)
}

what I need now is to call the super methods: super.viewWillAppear(animated) and super.viewWillDisappear(animated), but I don't know where or how, any suggestions?

Upvotes: 7

Views: 13960

Answers (2)

Karun Kumar
Karun Kumar

Reputation: 236

Your code will look like

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // Hide the navigation bar on the this view controller
    self.navigationController?.setNavigationBarHidden(true, animated: true)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    // Show the navigation bar on other view controllers
    self.navigationController?.setNavigationBarHidden(false, animated: true)
}

Upvotes: 13

user8377151
user8377151

Reputation:

The answer written by @Karun Kumar is correct, but it has a space after super. viewwillappear and viewwilldisapper.

Correct code is

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // Hide the navigation bar on the this view controller
    self.navigationController?.setNavigationBarHidden(true, animated: true)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    // Show the navigation bar on other view controllers
    self.navigationController?.setNavigationBarHidden(false, animated: true)
}

Upvotes: 0

Related Questions