在iOS13中,状态栏背景色与大文本模式下的导航栏不同

ste*_*ven 37 storyboard ios uistoryboard ios13

问题演示

重现问题的前提条件:

  1. Xcode 11 Beta + iOS 13(最新版本至2019年6月12日)
  2. 导航栏处于大文本模式
  3. 指定导航栏的颜色。

在实际设备中,状态栏将在绿色导航栏上方保留为白色。

我尝试的解决方案:

  1. 将其还原回iOS12即可解决,但最终我们将遇到iOS13 ...
  2. 禁用大文本模式将解决此问题...
  3. 隐藏状态栏将解决此问题,但会导致状态文本与导航栏项重叠。

有任何想法吗?感谢您的帮助。

Mik*_*ike 92

No hacks or funkiness required here. The key is defining the desired appearance and setting this value on BOTH the nav bar's standardAppearance AND its scrollEdgeAppearance. I have the following in the init for my base navigation controller subclass for my entire app:

if #available(iOS 13.0, *) {
    let navBarAppearance = UINavigationBarAppearance()
    navBarAppearance.configureWithOpaqueBackground()
    navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
    navBarAppearance.backgroundColor = <insert your color here>
    navigationBar.standardAppearance = navBarAppearance
    navigationBar.scrollEdgeAppearance = navBarAppearance
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 该方法存在一个问题,即当应用程序在后台运行时,用户何时更改其主题(例如light-&gt; dark)。其他内容会更改其颜色,但仅保留导航栏的颜色。您可以轻松地看到这一点。只需在模拟器中运行应用程序,然后从“设置”应用程序中的“开发人员”设置更改外观即可。尝试覆盖`traitCollectionDidChange`仍然出现错误。有什么想法吗? (3认同)
  • @CenoX,我找到了解决您问题的简便方法。首先,在资产目录中创建新的颜色集。将外观从“无”更改为“任何,光,暗”。选择您想要的任何颜色。第二步是在代码中加载您的自定义UIColor。使用“ let customColor = UIColor(named” CustomColor“),将名称” CustomColor“更改为资产名称。最后一步是将颜色分配给我们的条形背景。 (2认同)

Han*_*hel 12

在iOS 13上,根据Apple人机界面指南,使用大标题的导航栏具有透明的颜色。在这里查看更多信息:

在iOS 13及更高版本中,默认情况下,大型标题导航栏不包含背景材料或阴影。另外,随着人们开始滚动内容,大型标题会转换为标准标题


Chr*_*irk 11

通用代码

        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithOpaqueBackground()
        navBarAppearance.backgroundColor = // your color
        navBarAppearance.shadowImage = nil // line
        navBarAppearance.shadowColor = nil // line
        UINavigationBar.appearance(whenContainedInInstancesOf: [UINavigationController.self]).standardAppearance = navBarAppearance
        UINavigationBar.appearance(whenContainedInInstancesOf: [UINavigationController.self]).scrollEdgeAppearance = navBarAppearance
Run Code Online (Sandbox Code Playgroud)


Fab*_*bio 11

我的导航栏扩展,iOS 13 Swift 5

extension UIViewController {
func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
    if #available(iOS 13.0, *) {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithOpaqueBackground()
        navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
        navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
        navBarAppearance.backgroundColor = backgoundColor

        navigationController?.navigationBar.standardAppearance = navBarAppearance
        navigationController?.navigationBar.compactAppearance = navBarAppearance
        navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance

        navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
        navigationController?.navigationBar.isTranslucent = false
        navigationController?.navigationBar.tintColor = tintColor
        navigationItem.title = title

    } else {
        // Fallback on earlier versions
        navigationController?.navigationBar.barTintColor = backgoundColor
        navigationController?.navigationBar.tintColor = tintColor
        navigationController?.navigationBar.isTranslucent = false
        navigationItem.title = title
    }
}}
Run Code Online (Sandbox Code Playgroud)

如何使用:

configureNavigationBar(largeTitleColor: .yourColor, backgoundColor: .yourColor, tintColor: .yourColor, title: "YourTitle", preferredLargeTitle: true)
Run Code Online (Sandbox Code Playgroud)

如果你想要轻量内容,在 info.plist 中将基于 ViewController 的状态栏......设置为 NO

如果您不希望 largeTitles 将其设置为 false

在 iOS 13 上测试过,希望这会有所帮助:)


moh*_*bid 8

Objective C 解决方案和 iOS 13

UINavigationBarAppearance* navBarAppearance = [self.navigationController.navigationBar standardAppearance];
        [navBarAppearance configureWithOpaqueBackground];
        navBarAppearance.titleTextAttributes = @{NSForegroundColorAttributeName:TitleColor};
        navBarAppearance.largeTitleTextAttributes = @{NSForegroundColorAttributeName: TitleColor};
        navBarAppearance.backgroundColor = TopColor;
        self.navigationController.navigationBar.standardAppearance = navBarAppearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = navBarAppearance;
Run Code Online (Sandbox Code Playgroud)


mat*_*att 7

如果问题是您希望在显示大标题时为导航栏提供颜色,请使用新的UINavigationBarAppearance类。

    let app = UINavigationBarAppearance()
    app.backgroundColor = .blue
    self.navigationController?.navigationBar.scrollEdgeAppearance = app
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您想删除导航栏下方的下划线

if #available(iOS 13.0, *) {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithOpaqueBackground()
        navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.backgroundColor = <yourColor>
        navBarAppearance.backgroundImage = UIImage()
        navBarAppearance.shadowImage = UIImage()
        navBarAppearance.shadowColor = .clear
        self.navigationController?.navigationBar.standardAppearance = navBarAppearance
        self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
   
    }
Run Code Online (Sandbox Code Playgroud)