如何更改navigationBar的bar按钮项目颜色与初始颜色不同?

时间:2019-06-24 08:23:45

标签: ios swift uinavigationcontroller uinavigationbar swift4.2

我已经从视图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]
}

为什么上述方法不起作用?

1 个答案:

答案 0 :(得分:1)

除了不起作用的原因之外,它不是交互式的。因此,如果用户向后滑动但未与导航动画同步,则它将无法正常工作。因此,如果您需要一个可行的交互式解决方案,请继续:

定义自定义颜色的协议:

/// 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 }
}

注意1 :我添加了文本颜色更改支持。

注意2 :我使用了我的一个项目代码作为该答案的基础,如果您看不到一些通用的命名等等,那么抱歉。

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