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

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

问题描述

我在不同视图中的 UINavigationBar 处使用了 2 种不同的条形颜色.我在两个视图中都使用该方法更改颜色:

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天全站免登陆