【问题标题】:Xcode 13 - Navigation bar and status bar text color changing in swift OS 15Xcode 13 - swift OS 15 中的导航栏和状态栏文本颜色变化
【发布时间】:2022-01-10 10:15:10
【问题描述】:

最近我将我的 Xcode 更新为 13,之后,我遇到了导航栏和状态栏的一些问题。我在我的视图控制器中使用标签栏。更新Xcode后,根据版本,添加了一些导航栏相关的代码。

if #available(iOS 15.0, *) {
     tableView.sectionHeaderTopPadding = 0

     let appearance = UINavigationBarAppearance()
     appearance.configureWithOpaqueBackground()
     appearance.backgroundColor = UIColor(red: 58/255,green: 24/255, blue: 93/255, alpha: 1.0)
     appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]

     // Customizing our navigation bar
     navigationController?.navigationBar.tintColor = .white
     navigationController?.navigationBar.barTintColor = .white
     navigationController?.navigationBar.standardAppearance = appearance
     navigationController?.navigationBar.scrollEdgeAppearance = appearance
}

当我第一次打开应用程序时,一切正常。当我单击另一个选项卡然后单击此选项卡时。状态栏文本颜色正在改变。

我尝试了不同的方法来设置状态栏文本颜色。但没有什么对我有用。

【问题讨论】:

    标签: swift xcode storyboard uinavigationbar ios15


    【解决方案1】:

    使用此功能:

    extension YourViewController {
        override var preferredStatusBarStyle: UIStatusBarStyle {
            .lightContent
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果您只想将整个项目中的状态栏设置为白色,您可以通过将以下键添加到 project.plist 文件中轻松实现:

      <key>UIViewControllerBasedStatusBarAppearance</key>
      <false/>
      <key>UIStatusBarStyle</key>
      <string>UIStatusBarStyleLightContent</string>
      <key>UIUserInterfaceStyle</key>
      <string>Light</string>
      

      那么,这些键有什么作用...您只是定义状态栏颜色将不受任何视图控制器控制,并且默认颜色将是“浅色主题”,几乎是白色在这种情况下你想要...

      如果您不打算到处更新状态栏颜色,这将是最好的方法。

      这已经过测试,并且在 Xcode 13 和 iOS [13.x,14.x,15.x] 上按预期工作

      如果您想更新以自定义或更新导航栏的颜色,您应该能够使用如下所示的扩展程序,仅举几个例子,但你们应该明白我的意思:

      extension UINavigationBar {
      
      func clearBackground() {
          let navBarAppearance = UINavigationBarAppearance()
          navBarAppearance.configureWithTransparentBackground()
          navBarAppearance.shadowColor = UIColor.clear
          navBarAppearance.titleTextAttributes       = [NSAttributedString.Key.foregroundColor: UIColor.white]
          self.standardAppearance = navBarAppearance
          self.scrollEdgeAppearance = navBarAppearance
      }
      
      func blackColor() {
          // Create an Appearance and customise as you wish.
          // in this case just a black background with titles in white.
          let navBarAppearance = UINavigationBarAppearance()
          navBarAppearance.configureWithOpaqueBackground()
          navBarAppearance.shadowColor = UIColor.black
          navBarAppearance.titleTextAttributes       = [NSAttributedString.Key.foregroundColor: UIColor.white]
          self.standardAppearance = navBarAppearance
          self.scrollEdgeAppearance = navBarAppearance
      }
      

      }

      使用此代码,您应该能够通过调用以下代码轻松更新任何视图控制器:

      override func viewDidLoad() {
          super.viewDidLoad()
          // set the navigation bar color as transparent  
          self.navigationController?.navigationBar.clearBackground()
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多