iOS 15 UITabBarController的tabBar背景色变黑

Zhe*_*ang 83 uitabbarcontroller uitabbar ios swift ios15

tabBar.barTintColor在 iOS 15 beta 4 中无法更改。

背景。我们在 App Store 中有一个应用程序,每年在新的 iOS 主要版本发布之前,我们都会下载 iOS 测试版并测试我们的应用程序以提前解决问题。

我们的问题。今年,在 iOS 15 beta 4 中进行测试时,我们发现 UITabBarController 的 tabBar 背景颜色变成黑色,导致项目(图标和标题)难以阅读。在我们的代码中,我们有 self.tabBar.barTintColor = .white,这行代码在 iOS 15 中不起作用。

我们的尝试。我在网上搜索并发现了一个类似但不完全相同的问题报告,https://developer.apple.com/forums/thread/682420。我尝试过standardAppearance,但这不是解决方案,因为appearance我无法更改tabBar.tintColor

ano*_*erd 94

我遇到了同样的问题,并发现了与您的问题相同的链接。我对标签栏使用了相同的方法。

这是我正在使用的代码,它工作得很好。

if #available(iOS 15.0, *) {
   let appearance = UITabBarAppearance()
   appearance.configureWithOpaqueBackground()
   appearance.backgroundColor = customColor
   
   self.tabController.tabBar.standardAppearance = appearance
   self.tabController.tabBar.scrollEdgeAppearance = view.standardAppearance
}
Run Code Online (Sandbox Code Playgroud)

  • `tabBar.scrollEdgeAppearance` 是 iOS 15 上的一个新 API,必须设置该 API,为该属性设置相同的外观“修复”了该问题 (12认同)
  • tabBarController?.tabBar.scrollEdgeAppearance = tabBarController?.tabBar.standardAppearance (3认同)

Cha*_*man 59

与上面的答案类似,但修复了如果您使用自定义类则无法识别视图的问题:

if #available(iOS 15.0, *) {
    let appearance = UITabBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.backgroundColor = .white
    tabBar.standardAppearance = appearance
    tabBar.scrollEdgeAppearance = tabBar.standardAppearance
}
Run Code Online (Sandbox Code Playgroud)

  • 最好使用 `.systemBackground` 作为 `backgroundColor`,因为它会在深色模式下自动更改为黑色。 (2认同)

Dan*_*orm 23

创建UITabBarAppearance这样的内容以保持与以前的 iOS 版本相同的视觉行为:

@available(iOS 15.0, *)
private func updateTabBarAppearance() {
    let tabBarAppearance: UITabBarAppearance = UITabBarAppearance()
    tabBarAppearance.configureWithOpaqueBackground()
    
    let barTintColor: UIColor = .white
    tabBarAppearance.backgroundColor = barTintColor
    
    updateTabBarItemAppearance(appearance: tabBarAppearance.compactInlineLayoutAppearance)
    updateTabBarItemAppearance(appearance: tabBarAppearance.inlineLayoutAppearance)
    updateTabBarItemAppearance(appearance: tabBarAppearance.stackedLayoutAppearance)
    
    self.tabBar.standardAppearance = tabBarAppearance
    self.tabBar.scrollEdgeAppearance = tabBarAppearance
}

@available(iOS 13.0, *)
private func updateTabBarItemAppearance(appearance: UITabBarItemAppearance) {
    let tintColor: UIColor = .red
    let unselectedItemTintColor: UIColor = .green
    
    appearance.selected.iconColor = tintColor
    appearance.normal.iconColor = unselectedItemTintColor
}
Run Code Online (Sandbox Code Playgroud)


小智 23

我尝试了上面的答案是正确的。我想在这些解决方案中添加更多属性。我的要求是更改选项卡栏的背景颜色,更改选定的图像和标题颜色,更改未选定的图像和标题颜色。我可以使用下面的代码在iOS 15中实现它。

if #available(iOS 15.0, *){
    let appearance = UITabBarAppearance()
    appearance.configureWithDefaultBackground()
    appearance.backgroundColor = UIColor.appDarkColorLightShade
    
    appearance.compactInlineLayoutAppearance.normal.iconColor = .lightText
    appearance.compactInlineLayoutAppearance.normal.titleTextAttributes = [.foregroundColor : UIColor.lightText]
    
    appearance.inlineLayoutAppearance.normal.iconColor = .lightText
    appearance.inlineLayoutAppearance.normal.titleTextAttributes = [.foregroundColor : UIColor.lightText]
    
    appearance.stackedLayoutAppearance.normal.iconColor = .lightText
    appearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor : UIColor.lightText]
    
    self.tabBarController?.tabBar.standardAppearance = appearance
    self.tabBarController?.tabBar.scrollEdgeAppearance = self.tabBarController?.tabBar.standardAppearance
    self.tabBarController?.tabBar.tintColor = .white
    
}else{
    self.tabBarController?.tabBar.barTintColor = .appDarkColorLightShade
    self.tabBarController?.tabBar.unselectedItemTintColor = .lightText
    self.tabBarController?.tabBar.tintColor = .white
    
}
Run Code Online (Sandbox Code Playgroud)


Lew*_*s42 15

iOS 15:我们看到了这一点。这是我在故事板中更改的内容以使其发挥作用: 在此输入图像描述

这稍微改变了 iOS 14 中的外观,但足以满足我们的需求。


Raf*_*sun 9

对我来说这很简单,您不需要访问UINavigationController实例。只需从以下位置调用它AppDelegate:didFinishLaunchingWithOptions

if #available(iOS 15.0, *) {
    let appearance = UITabBarAppearance()
    appearance.configureWithOpaqueBackground()
    UITabBar.appearance().standardAppearance = appearance
    UITabBar.appearance().scrollEdgeAppearance = appearance
}
Run Code Online (Sandbox Code Playgroud)


Tab*_*kov 6

我的标签栏是透明的。这tabBar.scrollEdgeAppearance = tabBar.standardAppearance对我来说不起作用。

我不得不将其替换为tabBar.scrollEdgeAppearance = appearance. 我认为导航栏也是如此。

所以最终的修复看起来像:

   if #available(iOS 15.0, *) {
        let appearance = UITabBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = .white //or whatever your color is
        
        tabBar.scrollEdgeAppearance = appearance
        tabBar.standardAppearance = appearance
    }
Run Code Online (Sandbox Code Playgroud)

如果您想在应用程序委托中设置外观,上面的Rafat touqir Rafsun的答案也可以很好地工作。

在 iOS 15.0.1、Xcode 13 上测试。


Gle*_*enn 5

UITabBar这是我针对和的实现,Objective-C UINavigationBar。它们几乎是一样的。

didFinishLaunchingWithOptions在你的 中调用这个AppDelegate.m

- (void)setupAppearance {
  if (@available(iOS 15, *)) {
    UIColor *color = [UIColor whiteColor]; // #F5F5F5 for white smoke. 
    UITabBarAppearance *tabBarAppearance = [UITabBarAppearance new];
    tabBarAppearance.backgroundColor = color;
    [[UITabBar appearance] setStandardAppearance:tabBarAppearance];
    [[UITabBar appearance] setScrollEdgeAppearance:tabBarAppearance];
    
    UINavigationBarAppearance *navBarAppearance = [UINavigationBarAppearance new];
    navBarAppearance.backgroundColor = color;
    [[UINavigationBar appearance] setStandardAppearance:navBarAppearance];
    [[UINavigationBar appearance] setScrollEdgeAppearance:navBarAppearance];
  }
}
Run Code Online (Sandbox Code Playgroud)


Jay*_*ske 5

我的应用程序在表格视图下方有一个不透明的选项卡栏。barTintColor在 iOS 14.x 及更早版本中,在代理上设置就足够了UIAppearance,但在 iOS 15.0 中,当表格视图未到达屏幕底部时,选项卡栏背景为黑色。

我对 iOS 15 的解决方案是继续使用UIAppearance代理而不是较新的UITabBarAppearance类。我只需要设置backgroundColor除了barTintColor. 这至少向后兼容 iOS 11。

let tabBarAppearance = UITabBar.appearance()
tabBarAppearance.isTranslucent = false
tabBarAppearance.barTintColor = barColor
tabBarAppearance.backgroundColor = barColor

Run Code Online (Sandbox Code Playgroud)