UINavigationController hide only navigationBar - B

2020-05-08 08:29发布

问题:

I have three viewControllers in a UINavigationController. In the second one I need to hide the navigation bar but not the back button and other bar button. For this reason I can't use isNavigationBarHidden = true
Currently I am handling the above as follows :

First viewController :

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = Constants.kThemeRedColor
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.barStyle = .black
        self.navigationController?.navigationBar.isTranslucent = false
    }

Second viewController (hide only nav bar) :

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.isTranslucent = true
        self.navigationController?.view.backgroundColor = UIColor.clear
    } 

The issue here is that when I navigate from and to the firstViewController, for a very short duration when the 1st viewController is disappearing and appearing respectively, I see a black nav bar on it. I know this is because of the code written in 2nd viewController. But I don't have any other close solution of doing this. Attaching a screenshot :

First viewController (how it should be) :

Second viewController :

First viewController (with black nav bar for short duration) :

回答1:

In First viewController, also set the backgroundImage and shadowImage of navigationBar as nil, i.e.

class FirstVC: UIViewController
{
    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isTranslucent = true
        self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = nil
        self.navigationController?.navigationBar.barTintColor = .red
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.barStyle = .black
    }
}

class SecondVC: UIViewController
{
    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)

        self.navigationController?.navigationBar.isTranslucent = true
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        self.navigationController?.view.backgroundColor = UIColor.clear
        self.navigationController?.navigationBar.shadowImage = UIImage()
    }
}


回答2:

You should hide Navigation Controller's Navigation Bar and use custom navigation Bar using UIView. I think it will solve your issue.



回答3:

First viewController is showing with black nav bar for short duration on quick transition because of background color of UIWindow on which navigaion-bar transitions are happening. Simply, add this line in your didFinishLaunchingWithOptions: method of AppDelegate

window?.backgroundColor = Constants.kThemeRedColor

And you are done. Happy coding!