UINavigationBar在推送时改变颜色(UINavigationBar change colors on push)

IPhone IT屋
百度翻译此文   有道翻译此文
问 题

I'm using 2 different bar tint colors at UINavigationBar in different views. I'n changing color with that method in both views:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.barTintColor = COLOR
}

When I tap on back button color is not changed smoothly (you can see blink on last second).

Visual bug

But everything is okay if just swipe view back instead of tapping on back button.

No visual bug

How to make smooth transition in both situations?

解决方案

I've coded final solution that looks most comfortable to use (don't need to use a lot of overrides in own view controllers). It works perfectly at iOS 10 and easy adoptable for own purposes.

GitHub

You can check GitHub Gist for full class code and more detailed guide, I won't post full code here because Stackoverflow is not intended for storing a lot of code.

Usage

Download Swift file for GitHub. To make it work just use ColorableNavigationController instead of UINavigationController and adopt needed child view controllers to NavigationBarColorable protocol.

Example:

class ViewControllerA: UIViewController, NavigationBarColorable {
    public var navigationBarTintColor: UIColor? { return UIColor.blue }

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Push", style: .plain, target: self, action: #selector(self.showController))
    }

    func showController() {
        navigationController?.pushViewController(ViewControllerB(), animated: true)
    }
}

class ViewControllerB: UIViewController, NavigationBarColorable {
    public var navigationBarTintColor: UIColor? { return UIColor.red }
}

let navigationController = ColorableNavigationController(rootViewController: ViewControllerA())

本文地址:IT屋 » UINavigationBar change colors on push

问 题

我在不同的视图中使用 UINavigationBar 中的两种不同的色调颜色。我在两个视图中都用这种方法改变了颜色:



 覆盖func viewWillAppear(动画:Bool){
super .viewWillAppear(动画)
self.navigationController?.navigationBar.barTintColor = COLOR
}


当我点击后退按钮颜色没有顺利改变时(你可以看到最后一秒闪烁)。





但如果只是向后滑动视图而不是点击后退按钮,一切都会好的。





如何在两种情况下平滑过渡?


解决方案

我编写了最终解决方案看起来最舒服(不需要在自己的视图控制器中使用很多覆盖)。它在iOS 10上完美运行,易于用于自己的目的。



GitHub



你可以检查 GitHub Gist 完整的课程代码和更详细的指南,我不会发布完整的代码在这里,因为Stackoverflow不用于存储大量代码。



用法



下载GitHub的Swift文件。要使它工作只需使用 ColorableNavigationController 而不是 UINavigationController 并采用所需的子视图控制器 NavigationBarColorable 协议。



示例:



  class ViewControllerA:UIViewController,NavigationBarColorable {
public var navigationBarTintColor:UIColor? {return UIColor.blue}

override func viewDidLoad(){
super.viewDidLoad()

navigationItem.rightBarButtonItem = UIBarButtonItem(title:“Push”,style :.plain,target:self,action:#selector(self.showController))
}

func showController(){
navigationController?.pushViewController(ViewControllerB(),animated :true)
}
}

类ViewControllerB:UIViewController,NavigationBarColorable {
public var navigationBarTintColor:UIColor? {return UIColor.red}
}

let navigationController = ColorableNavigationController(rootViewController:ViewControllerA())

本文地址:IT屋 » UINavigationBar在推送时改变颜色

官方微信
扫一扫关注IT屋
微信公众号搜索 “ IT屋 ” ,选择关注
与百万开发者在一起