UINavigationController hide only navigationBar - Back animation issue

£可爱£侵袭症+ 提交于 2019-12-02 17:07:21

问题


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!



来源:https://stackoverflow.com/questions/46603451/uinavigationcontroller-hide-only-navigationbar-back-animation-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!