UINavigationBar 在推送时更改颜色

2023-06-13移动开发问题
3

本文介绍了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 在推送时更改颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)...
2024-08-12 移动开发问题
13