【问题标题】:UINavigationBar tint color does not updateUINavigationBar tint 颜色不更新
【发布时间】:2015-11-11 16:03:37
【问题描述】:

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

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()]

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

【问题讨论】:

    标签: ios swift colors uinavigationbar


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我只是在处理同样的问题,事实证明,如果您在运行时更改 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
      }
      

      【讨论】:

      • 这似乎对我不起作用。我做错了什么:func setupForColor(color: UIColor) { navigationBar.tintColor = color if darkMode == true { navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()] UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent } else { navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blackColor()] UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default } }
      • 在我的暗模式变换函数中:if let colorNavigationController = self.navigationController as? ColorNavigationController { if darkMode == true { colorNavigationController.setupForColor(UIColor(hexString: "3A3A3A")!) } else { colorNavigationController.setupForColor(UIColor(hexString: "#FFF8D3")!) } }
      • 你真的也改变了darkMode bool 值吗?
      • 是的,它包含在原始帖子的 if 语句中,但我认为不需要回答这个问题。 (抱歉回复晚了,MacBook 已经疯了:/)
      • 该代码对我有用,您是否在情节提要中正确设置了导航控制器类?为您制作示例项目:github.com/libec/StackOverflow/tree/master/02-Navigation
      猜你喜欢
      • 2011-06-29
      • 1970-01-01
      • 2013-10-30
      • 2020-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      相关资源
      最近更新 更多