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.
Came across this thread and came up with a nice solution to help all in the future.
Firstly create a custom
UINavigationController
with a typeenum
that will help define your navigation setups:Then create a custom
UIViewController
using thewillMove(...
:Then simply in your UIViewControllers, subclass your new
ViewController
and set thenavType
in theviewDidLoad
:Another solution that works is overriding pushViewController(_ viewController: UIViewController, animated: Bool) and popViewController(animated: Bool) -> UIViewController? of UINavigationController.
use this function in thiredVC
You can make custom navigation bar for SecondVC. And call -popViewController method manually on back button click.