Hols
Hols

Reputation: 391

Customise the navigation bar background for a single view in Swift?

I have an app with multiple views and controllers but on just one of those views I would like to make the top navigation bar transparent with white text. I have the following code in the controller for said view:

override func viewDidLoad() {
    super.viewDidLoad()

    let bar:UINavigationBar! =  self.navigationController?.navigationBar

    bar.tintColor = UIColor.whiteColor()
    bar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
    bar.shadowImage = UIImage()
    bar.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
}

When I open the app the top bar on the other views looks as expected and when I open this view the top bar looks like I want it to. But when I navigate from the this view to the others, the other views inherit the changes made by the above code to the top bar.

Is there a way to prevent this so that the navigation bar only changes for that particular view while leaving the rest intact?

Thanks in advance!

Upvotes: 3

Views: 1852

Answers (1)

rdelmar
rdelmar

Reputation: 104082

There is only one navigation bar for any particular navigation controller, so if you change it in one controller, it will be changed for all. The way to fix that is to change it back to whatever you want for the other controllers in viewWillDisappear (or viewDidDisappear). You might also need to move the code you show to view willAppear if you are coming back to this same instance when another controller gets popped.

Upvotes: 7

Related Questions