如何更改与初始颜色不同的导航栏按钮项目颜色?

How to change navigationBar bar button items color which is different from the initial color?

我已经从视图 A 导航到视图 B(使用新的导航控制器和黑色导航栏)

查看控制器:

    // With Navigation Controller
    let storyBoard: UIStoryboard = UIStoryboard(name: "ViewB", bundle: nil)
    let newViewController = storyBoard.instantiateViewController(withIdentifier: "ViewB") as! ViewBController
    let navCont = UINavigationController(rootViewController: newViewController)
    // Change the navigation bar to translucent
    navCont.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navCont.navigationBar.shadowImage = UIImage()
    navCont.navigationBar.isTranslucent = true
    navCont.navigationBar.tintColor = UIColor.black
    //present(navCont, animated: true, completion: nil)
    show(navCont, sender: nil)

从视图 B 导航到视图 C 时,我想将 navigationBar.tintColor 从黑色更改为白色。

视图 B 控制器:

@IBAction func staticQRBtnPressed(_ sender: Any) {
    // Without Navigation Controller
    let storyBoard: UIStoryboard = UIStoryboard(name: "ViewC", bundle: nil)
    let newViewController = storyBoard.instantiateViewController(withIdentifier: "ViewCController") as! ViewCController
    newViewController.navigationController?.navigationBar.barTintColor = UIColor.white
    newViewController.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
    self.show(newViewController, sender: nil) // Push to navigation stack
}

查看 C 控制器:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationController?.navigationBar.barTintColor = UIColor.white
    navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}

为什么以上方法都不行?

除了为什么它不起作用,它不是交互式的。因此,如果用户向后滑动且未与导航动画同步,它将无法正常工作。因此,如果您想要一个有效的交互式解决方案,我们开始吧:

定义自定义颜色协议:

/// Navigation bar colors for `ColorableNavigationController`, called on `push` & `pop` actions
public protocol NavigationBarColorable: class {
    var navigationTintColor: UIColor? { get }
    var navigationBarTintColor: UIColor? { get }
}

public extension NavigationBarColorable {
    var navigationTintColor: UIColor? { return nil }
}

子类 UINavigationController 并覆盖导航方法:

/**
 UINavigationController with different colors support of UINavigationBar.
 To use it please adopt needed child view controllers to protocol `NavigationBarColorable`.
 - note: Don't forget to set initial tint and barTint colors
 */
class AppNavigationController: UINavigationController {    

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
        navigationBar.shadowImage = UIImage()
        if let colors = rootViewController as? NavigationBarColorable {
            setNavigationBarColors(colors)
        }
    }

    private var previousViewController: UIViewController? {
        guard viewControllers.count > 1 else {
            return nil
        }
        return viewControllers[viewControllers.count - 2]
    }

    override open func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if let colors = viewController as? NavigationBarColorable {
            setNavigationBarColors(colors)
        }

        setTabBarHidden(viewController is TabBarHidable)

        super.pushViewController(viewController, animated: animated)
    }

    override open func popViewController(animated: Bool) -> UIViewController? {
        if let colors = previousViewController as? NavigationBarColorable {
            setNavigationBarColors(colors)
        }

        setTabBarHidden(previousViewController is TabBarHidable)

        // Let's start pop action or we can't get transitionCoordinator()
        let popViewController = super.popViewController(animated: animated)

        // Secure situation if user cancelled transition
        transitionCoordinator?.animate(alongsideTransition: nil, completion: { [weak self] context in
            guard let `self` = self else { return }
            self.setTabBarHidden(self.topViewController is TabBarHidable)
            guard let colors = self.topViewController as? NavigationBarColorable else { return }
            self.setNavigationBarColors(colors)
        })

        return popViewController
    }

    override func popToRootViewController(animated: Bool) -> [UIViewController]? {
        if let colors = rootViewController as? NavigationBarColorable {
            setNavigationBarColors(colors)
        }

        let controllers = super.popToRootViewController(animated: animated)

        return controllers
    }

    private func setNavigationBarColors(_ colors: NavigationBarColorable) {

        if let tintColor = colors.navigationTintColor {
            navigationBar.titleTextAttributes = [
                .foregroundColor : tintColor,
                .font : R.font.iranSansFaNumBold(size: 14)!
            ]
            navigationBar.tintColor = tintColor
        }

        navigationBar.barTintColor = colors.navigationBarTintColor
    }
}

并在每个你想要自定义导航颜色的视图控制器中遵循和实现协议方法:

extension MyCustomViewController: NavigationBarColorable {
    public var navigationBarTintColor: UIColor? { return .red }
    public var navigationTintColor: UIColor? { return .blue }
}

Note1: 我添加了文本颜色更改支持。

注意 2:我使用了我的一个项目代码作为这个答案的基础,如果您看到一些 none 通用命名等,我们深表歉意

Note3: 正如我在代码中提到的,不要忘记设置初始色调和 barTint 颜色。