0

I just removed the navigation bar shadow line with the following code :

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()

When I use this code and I try to open another page which belongs to the same navigation controller, its navigation bar looks different. I'm trying to set the same navigation bar background color and tint color, but it doesn't work. However, when I remove these codes, all the pages that I use work normally. How can I fix this issue?

Screenshots :

MainViewController

SecondViewController

jorjj
  • 1,479
  • 4
  • 20
  • 36

2 Answers2

1

It's common behaviour. When you set backgroundImage then it's not possible to set new colour. You need to set setBackgroundImage to nil and then set new colour that you want inside next ViewController.

This library can help you to do it easily https://github.com/MoZhouqi/KMNavigationBarTransition PS: See an example by the link

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        configureAppearance()
    }

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

    func configureAppearance() {
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
    }
}


class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        configureAppearance()
    }

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

    func configureAppearance() {
        self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
        self.navigationController?.navigationBar.barTintColor = UIColor.yellow
    }
}
Nosov Pavel
  • 1,571
  • 1
  • 18
  • 34
  • I don't wanna use framework for this little problem. Can you help me that how I can fix this problem naturally? – jorjj Nov 27 '17 at 17:00
  • 1
    Yeah, set in first ViewController in viewWillAppear() your custom NavigationController appearance then in second ViewController set another NavigationController appearance. I added for you example above. – Nosov Pavel Nov 27 '17 at 17:36
  • thanks, man. I also fixed the problem with changing the status bar color and nav bar background color. – jorjj Nov 27 '17 at 18:39
  • @winnervswinner you are welcome! if you want to remove side effects that are showing when you going back to first view controller - use library that I have mentioned – Nosov Pavel Nov 27 '17 at 22:15
0

It looks like your navigation bar is translucent but your view controller doesn't extend behind it so you need the window which defaults to black.

Either

  1. Check the box to extend the VC under top bars in interface builder
  2. Change UIApplication.shared.keyWindow?.backgroundColor to .white
  3. Make your nav bar opaque
Josh Homann
  • 15,933
  • 3
  • 30
  • 33