Search code examples
iosswiftswift3uinavigationcontrolleruistoryboardsegue

Making only one navigation bar in Navigation Stack transparent iOS Swift


I have a navigationcontroller with navigation flows as shown below:

NC -> A -> B

B appears through a push segue.

The navigationbar of A is made transparent using following

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.navigationController!.navigationBar.isTranslucent = true
    self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController!.navigationBar.shadowImage = UIImage()
}

and is translucency is set to false in viewWillDisappear so that B can have the usual navigation bar:

override func viewWillDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    self.navigationController!.navigationBar.isTranslucent = false
}

The issue is that when Back button is pressed in B to return to A, The navigation bar of B appears momentarily before disappearing. How to solve this issue?

PS: I do not want to add code to overridden methods of B as B might be shared by other navigation controller.


Solution

  • The issue is that when Back button is pressed in B to return to A, The navigation bar of B appears momentarily before disappearing. How to solve this issue?

    You do not need to toggle anything in your viewWillDisappear method. Just toggle everything in your viewWillAppear method in your every screens.

    Is this what you want? If so, I made a sample project on Github just for you, and for other people who are new to iOS in the future.

    https://github.com/glennposadas/showhidenavbar-ios

    Though it uses my very simple cocoapod, you can just copy everything from my framework and sample project.

    enter image description here