64

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

4

16 回答 16

66

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

这是我正在使用的代码,它运行良好。

if #available(iOS 15.0, *) {
   let appearance = UITabBarAppearance()
   appearance.configureWithOpaqueBackground()
   appearance.backgroundColor = customColor
   
   self.tabController.tabBar.standardAppearance = appearance
   self.tabController.tabBar.scrollEdgeAppearance = view.standardAppearance
}
于 2021-08-11T23:13:52.807 回答
34

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

if #available(iOS 15.0, *) {
    let appearance = UITabBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.backgroundColor = .white
    tabBar.standardAppearance = appearance
    tabBar.scrollEdgeAppearance = tabBar.standardAppearance
}
于 2021-09-20T21:08:24.103 回答
21

创建一个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
}
于 2021-09-15T21:34:59.530 回答
18

我尝试了以上正确的答案。我想在这些解决方案中添加更多属性。我的要求是更改标签栏的背景颜色,更改选定的图像和标题颜色,更改未选定的图像和标题颜色。我能够使用以下代码在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
    
}
于 2021-09-22T11:16:18.947 回答
11

iOS 15:我们看到了这一点。这是我在情节提要中进行的更改以使其正常工作: 在此处输入图像描述

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

于 2021-09-04T10:24:02.313 回答
6

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

if #available(iOS 15.0, *) {
    let appearance = UITabBarAppearance()
    appearance.configureWithOpaqueBackground()
    UITabBar.appearance().standardAppearance = appearance
    UITabBar.appearance().scrollEdgeAppearance = appearance
}
于 2021-10-07T14:10:24.693 回答
5

我的标签栏是透明的。而这个: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
    }

如果您想在 appdelegate 中设置外观,上述 Rafat touqir Rafsun 的回答也可以正常工作。

在 iOS 15.0.1、Xcode 13 上测试。

于 2021-10-08T10:38:44.597 回答
3

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

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

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

于 2021-10-21T22:23:12.737 回答
3

对于 Swift,我使用下面的代码来保持我的应用程序与以前相同的外观。我的标签栏为图标和标题选择了粉红色。并使用默认的灰色调。

使用configureWithDefaultBackground而不是configureWithOpaqueBackground因为我想要标签栏的一点透明度。

目前,它运行良好,继续寻找最新的变化。

       if #available(iOS 15, *) {
            let appearance = UITabBarAppearance()
            appearance.configureWithDefaultBackground()
            appearance.stackedLayoutAppearance.normal.iconColor = .systemGray
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.systemGray]
            
            appearance.stackedLayoutAppearance.selected.iconColor = .systemPink
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.systemPink]
            // appearance.backgroundColor = .systemBackground
            
            self.tabBar.standardAppearance = appearance
            self.tabBar.scrollEdgeAppearance = appearance
        }
        
        if #available(iOS 13, *) {
            let appearance = UITabBarAppearance()
            appearance.shadowImage = UIImage()
            appearance.shadowColor = .white
            
            appearance.stackedLayoutAppearance.normal.iconColor = .systemGray
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.systemGray]
//            appearance.stackedLayoutAppearance.normal.badgeBackgroundColor = .yellow
            
            appearance.stackedLayoutAppearance.selected.iconColor = .systemPink
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.systemPink]

            self.tabBar.standardAppearance = appearance
        }
于 2021-10-12T09:11:25.950 回答
2
  if #available(iOS 15.0, *) {
    navigationController?.view.backgroundColor = UIColor.colorName
  }
于 2021-09-26T10:53:29.530 回答
2

Xcode 13.0 - iOS 15.0

我的目标是在视图控制器更改时动态更新导航栏的色调颜色。我首先设置了这些设置。然后在需要时使用我想使用的 UIColor 调用此函数。

称呼:

setNavigationBarAppearance(color: .green)

扩大:

// MARK: Navigation Bar Appearance Function
extension MainViewController {
    func setNavigationBarAppearance(color: UIColor) {
        if #available(iOS 15.0, *){
            let appearance = UINavigationBarAppearance()
            appearance.configureWithDefaultBackground()
            appearance.backgroundColor = color // The background color.
            
            self.navigationController?.navigationBar.standardAppearance = appearance
            self.navigationController?.navigationBar.scrollEdgeAppearance = self.navigationController?.navigationBar.standardAppearance
            
        } else { // Background color support for older versions
            self.navigationController?.navigationBar.barTintColor = color
            
        }
    }
}

编辑:在使用这些设置时,导航栏的色调在早期设备上不起作用,对于错误信息,我们深表歉意。恢复以下设置可使色调颜色在所有版本中都按预期工作。

我的设置(Tint 颜色在 iOS 13.5 和 iOS 15 上完美运行):

在此处输入图像描述

于 2021-09-23T12:14:18.077 回答
2

这是我的实现,Objective-C,用于UITabBarUINavigationBar. 它们几乎相同。

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];
  }
}
于 2021-10-11T14:08:19.757 回答
2

更新到 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
 }
于 2021-10-26T10:39:30.470 回答
0

如果您不想设置scrollEdgeAppearance,您可以将其覆盖为standardAppearance,您的所有设置都将同时显示。

if #available(iOS 15.0, *) {
   tabBar.scrollEdgeAppearance = tabBar.standardAppearance
}
于 2021-10-08T14:34:44.027 回答
0
     func setupAppearance() {
            // Update based on your font requirements
            let font = UIFont.systemFont(ofSize: 12, weight: .bold)
            let tabBarAppearance = UITabBarAppearance()
            let tabBarItemAppearance = UITabBarItemAppearance()
            
            tabBarItemAppearance.normal.titleTextAttributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: UIColor.gray]
            tabBarItemAppearance.selected.titleTextAttributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: UIColor.black]

            /* Note: To reset background and shadow properties to display opaque colors can use - tabBarAppearance.configureWithOpaqueBackground() */
            tabBarAppearance.backgroundColor = .white
            tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance
            
            tabBar.standardAppearance = tabBarAppearance
        if #available(iOS 15.0, *) {
            tabBar.scrollEdgeAppearance = tabBarAppearance
        }
    }
于 2021-11-26T16:23:13.127 回答
-1

UITabBarController 的内部子类

if #available(iOS 15.0, *) {
            let appearance = UITabBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = .white
            tabBar.standardAppearance =  appearance
            tabBar.scrollEdgeAppearance = tabBar.standardAppearance
        }
于 2021-09-24T14:36:32.770 回答