UINavigationBar色调颜色不会更新

时间:2015-08-18 15:54:52

标签: ios swift colors uinavigationbar

我正在我的应用中实施黑暗模式。这是我的代码(我在双击屏幕时调用):

if darkMode == false {
UINavigationBar.appearance().tintColor = UIColor(hexString: "#3A3A3A")
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
} else {
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
UINavigationBar.appearance().barTintColor = UIColor(hexString: "#FFFDF3")
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.blackColor()]

唯一更新的是我的状态栏,但导入栏在我进入另一个视图并返回主视图后会更新。这是为什么?我有什么不对的吗?

2 个答案:

答案 0 :(得分:0)

我刚刚处理同样的问题,如果你在运行时更改appearance()代理,它就没有任何效果。您需要直接更改实例的属性。所以你需要做的是使用子方法UINavigationBarController和你设置颜色和状态栏外观的方法,例如:

class ColorNavigationController: UINavigationController {

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        setupForColor(UIFactory.sharedInstance.tintColor) //provides default color
    }

    func setupForColor(color: UIColor) {
        navigationBar.tintColor = color
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
        UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent

    }
}

然后当你双击屏幕时:

if let colorNavigationController = self.navigationController as? ColorNavigationController {
    colorNavigationController.setupForColor(UIColor.redColor) // based on your current scheme
}

答案 1 :(得分:0)

知道了。您无法在运行时更改appearance(),但您可以执行navigationController?.navigationBar.tintColor = UIColor.redColor()