在升级到 XCode 13(和 iOS 15)后,选项卡和导航栏发生了变化。

22
我有一个iOS应用程序,自从升级到Xcode 13后,我注意到了一些关于选项卡和导航栏的奇怪更改。在Xcode 13中,选项卡和导航栏上现在有这个黑色区域,并且启动应用程序时,选项卡栏现在是黑色的,导航栏也是黑色的。令人奇怪的是,如果视图具有滚动或表格视图,则向上滚动,底部选项卡栏会恢复其白色颜色,向下滚动,导航栏会恢复其白色颜色。
注意:我已经强制使用iOS 13及以上的浅色主题。
 if #available(iOS 13.0, *) {
     window!.overrideUserInterfaceStyle = .light
 }

请问有谁能帮忙或者指点一下我如何处理这个问题呢?

是否有简单的解决方法可以让Storyboard自动调整,还是这种情况下需要手动更改每个视图呢?

升级前Storyboard的示例:

enter image description here

之后:

enter image description here

升级前后的模拟器屏幕(分别):

enter image description here

7个回答

27

关于导航栏是黑色的,请尝试做到:

let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .red
appearance.titleTextAttributes = [.font: 
UIFont.boldSystemFont(ofSize: 20.0),
                              .foregroundColor: UIColor.white]

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

我写了一篇新文章来探讨这个问题。

https://medium.com/@eduardosanti/uinavigationbar-is-black-on-ios-15-44e7852ea6f7


3
谢谢 @Eduardo Santi,上面的解决方案可以使用。在我的情况下,将 isTranslucent 设置为 true 也有效。对于选项卡栏,我需要指定一个背景颜色。 - tendai

16

更新到XCode 13和iOS 15之后,我也遇到了一些标签栏的问题,包括不同状态下的栏背景颜色和项目文本颜色。我解决的方法:

if #available(iOS 15, *) {
   let tabBarAppearance = UITabBarAppearance()
   tabBarAppearance.backgroundColor = backgroundColor
   tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: selectedItemTextColor]
   tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: unselectedItemTextColor]
   tabBar.standardAppearance = tabBarAppearance
   tabBar.scrollEdgeAppearance = tabBarAppearance
} else {
   UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: selectedItemTextColor], for: .selected)
   UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: unselectedItemTextColor], for: .normal)
   tabBar.barTintColor = backgroundColor
 }

12

对我来说,导航栏和选项卡栏都存在问题,因此我在AppDelegate中全局应用了样式。

func configureNavigationBarAppearance() {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.backgroundColor = .white
    UINavigationBar.appearance().standardAppearance = appearance
    UINavigationBar.appearance().scrollEdgeAppearance = appearance
}

func configureTabBarAppearance() {
    let appearance = UITabBarAppearance()
    appearance.backgroundColor = .white
    UITabBar.appearance().standardAppearance = appearance
    UITabBar.appearance().scrollEdgeAppearance = appearance
}

2
简单而华丽的解决方案... - Wahab Khan Jadon

1

在 XCode13.0 和 iOS 15.0 中,通过编程方式解决了选项卡栏和导航栏透明度问题,已经完美解决。

对于选项卡栏:

if #available(iOS 15, *) {
        let appearance = UITabBarAppearance()
        appearance.configureWithOpaqueBackground()
        UITabBar.appearance().standardAppearance = appearance
        UITabBar.appearance().scrollEdgeAppearance = appearance
 }

关于NavigationBar

 if #available(iOS 15, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        UINavigationBar.appearance().standardAppearance = appearance
        UINavigationBar.appearance().scrollEdgeAppearance = appearance
 }

尝试了一下,现在我的状态栏变成白色了。我们没有使用任何故事板,虽然这段代码将我的状态栏从灰色(我认为是来自场景窗口)改为白色,但它并不遵循视图的背景颜色,而这正是状态栏应该继承的,不是吗? - justdan0227

1
您可以在storyboards中选择Tab Bar,然后在属性检查器中选择标准和滚动边缘外观,并将它们的设置都设为iOS 13,并且对于自定义字体或颜色,您需要将标准布局出现堆叠更改为自定义并设置属性。
对于导航栏,您可以在属性检查器中类似地设置标准和滚动边缘外观,但这在其他地方已经在stack overflow中提到过。 enter image description here

2
如果你的应用程序支持iOS12及以下版本,这将导致崩溃。 - Yaroslav Dukal

0

首先,问题是由于取消半透明引起的。 我通过从属性检查器滚动边缘选择导航栏外观来解决它。 这将修复它 请查看此屏幕截图


很有趣的是,在我的端上,实际检查半透明解决了一半的问题。 - tendai
我知道这是一团糟:D - Khaled Samer Elsedek

0
我的问题已经解决了,只需要按照以下步骤进行操作:将右侧的颜色替换为您想要的导航栏颜色。
navigationController?.navigationBar.backgroundColor = .lightGray

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接