Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios swift - navigation item background turns black when going back to a screen where the navigation bar is hidden

I thought showing a screenshot would help understand the issue a bit better.

So the context is the following:

I'm in a navigation controller, on the settings screen of the app (which has a navigation item) and when we tap on the back button, we go back to the main screen of the app (for which I've hidden the navigation bar in the viewWillAppear of the main screen because I'm building a custom header view myself).

At soon as I tap on the back button, the navigation bar disappears immediately and I see a black rectangle appears instead until the animation to display the main screen is completed.

Do you know how I can avoid having this black rectangle appear?

Hope the questions makes sense.

Screenshots

Here is the initial settings screen:

enter image description here

When we tape on the back button, this happens... help :D

I know this piece of code is most likely responsible for the error, but I absolutely need to have the navigationBar hidden on the previous screen.

override func viewWillAppear(_ animated: Bool) {

navigationController?.isNavigationBarHidden = true

}

enter image description here

like image 619
Edouard Barbier Avatar asked Oct 20 '16 23:10

Edouard Barbier


People also ask

How do I change the color of the navigation bar back button in Swift?

To change the color of the navigation bar and the title text, we can use UINavigationBarAppearance, which is available since iOS 13. We hereby make a class Theme with a static method as follows, which will come in handy when we need it for some setup in our View or elsewhere.

How do I customize the navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.


3 Answers

Add below code in Second ViewController

the color can corresponding your custom

 override func viewWillAppear(_ animated: Bool) {
              self.navigationController?.view.backgroundColor = UIColor.white


        }
like image 126
Puji Wahono Avatar answered Oct 28 '22 02:10

Puji Wahono


For Swift3.0

Add below code in First ViewController

override func viewWillAppear(_ animated: Bool) {
     super.viewWillAppear(true)
     navigationController?.setNavigationBarHidden(true, animated: animated)
}

Add below code in Second ViewController

func backButtonPressed() {
    navigationController?.setNavigationBarHidden(false, animated: false)
    navigationController?.popViewController(animated: true)
}
like image 21
Chandu kumar.Alasyam Avatar answered Oct 28 '22 00:10

Chandu kumar.Alasyam


Have you tried the animated method of hiding the navigation bar setNavigationBarHidden(_ hidden: Bool, animated: Bool)?

like image 43
paulvs Avatar answered Oct 28 '22 00:10

paulvs