UINavigationBar在推送时改变颜色 [英] UINavigationBar change colors on push

查看:274
本文介绍了UINavigationBar在推送时改变颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

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

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

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

How to make smooth transition in both situations?

推荐答案

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

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 Gist 完整的课程代码和更详细的指南,我不会发布完整的代码在这里,因为Stackoverflow不用于存储大量代码。

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.

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

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

示例:

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())

这篇关于UINavigationBar在推送时改变颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆