Search code examples
iosswiftuinavigationbar

Changing navigation bar color while popping view controller


I have three view controllers. In the first view controller (FirstVC), the navigation bar's bar tint color is clearColor and the bar itself is translucent. When I click on something, I push to SecondVC where the navigation bar needs to be opaque. So I set the barTintColor to some color value and set isTranslucent to false. When I push to ThirdVC from SecondVC, the navigation bar again needs to be translucent. The issue arises when I pop the ThirdVC and go back to SecondVC. The navigation bar appears as transparent for a second and then becomes opaque as required. I'm unable to understand the reason for the delay.

The following method is called from viewWillAppear() of SecondVC. I have tried doing the same from the viewWillDisappear()of ThirdVC, but to no effect.

fileprivate func configureNavigationBar(){

        self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = nil
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.barTintColor = Style.Movie.primaryBackgroundColor
        let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.white]
        self.navigationController?.navigationBar.titleTextAttributes = titleDict as? [String:Any]
    }

It works fine if I swipe back instead of tapping on the back button.


Solution

  • use this function in thiredVC

    override func willMove(toParentViewController parent: UIViewController?) {
              self.navigationController?.navigationBar.barTintColor = color use in secondVC
        }