【问题标题】:Cant change the color of programmatically created UINavigationController on dark mode无法在暗模式下更改以编程方式创建的 UINavigationController 的颜色
【发布时间】:2021-01-04 21:33:31
【问题描述】:

我正在以编程方式创建导航视图,我希望在深色模式下使用白色导航栏。我以为我做的一切都是正确的,但在黑暗模式下,我的导航栏仍然变暗

 let navVC = UINavigationController(rootViewController: weatherMain)
 navVC.navigationController?.navigationBar.tintColor = .white
 navVC.navigationController?.navigationBar.backgroundColor = .white
 navVC.modalPresentationStyle = .fullScreen
 present(navVC, animated: true, completion: nil)

虽然有些答案建议使用 .backgroundColor 或 .tintColor,但它们对我一点帮助都没有。不知道怎么解决

【问题讨论】:

    标签: swift uinavigationcontroller ios-darkmode


    【解决方案1】:

    您可以在 AppDelegate 文件中使用它来帮助您更改颜色:

    UINavigationBar.appearance().tintColor = #colorLiteral(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1)
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1)]
    

    【讨论】:

      【解决方案2】:

      我想你在找preferredStatusBarStyle

      也许您可以尝试为 UINavigationController 覆盖这些函数:

      override var preferredStatusBarStyle: UIStatusBarStyle {
          if traitCollection.userInterfaceStyle == .light {
              return UIStatusBarStyle.darkContent
          } else {
              return UIStatusBarStyle.lightContent
          }
      }
      
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(false)
          setNeedsStatusBarAppearanceUpdate()
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-02-09
        • 2012-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-28
        相关资源
        最近更新 更多