UITabBarAppearance 在 iOS15 iPad 上不起作用(标题颜色)

问题描述 投票:0回答:4

我创建了一个简单的演示,只创建了一个 UITabBarController 的子类并在故事板中设置。

我想在选择时将 TabBarButtonItem 的标题设置为橙色,在正常情况下将标题设置为黑色。以下代码在 iPhone 上的任何 iOS 版本上都可以正常工作,但在 iOS 15 的 iPad(设备和模拟器)上,所选颜色更改为蓝色和有线正常状态颜色。

这是 Apple 的错误还是我错过了什么?(我使用的是 Xcode13)

class CustomViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let tabBarAppearnace = UITabBarAppearance()
        let tabFont =  UIFont.boldSystemFont(ofSize: 18)
        
        let selectedAttributes: [NSAttributedString.Key: Any]
        = [NSAttributedString.Key.font: tabFont, NSAttributedString.Key.foregroundColor: UIColor.orange]
        let normalAttributes: [NSAttributedString.Key: Any]
        = [NSAttributedString.Key.font: tabFont, NSAttributedString.Key.foregroundColor: UIColor.black]
        
        tabBarAppearnace.stackedLayoutAppearance.normal.titleTextAttributes = normalAttributes
        tabBarAppearnace.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttributes
        
        tabBar.standardAppearance = tabBarAppearnace
    }
}

ios swift uitabbaritem ios15
4个回答
12
投票

对于 iPadOS,您必须使用

inlineLayoutAppearance
属性,因为在 iPad 上,TabBar 中的项目默认情况下内联显示(标题和图标彼此相邻显示)。
但事实上,您还应该配置
compactInlineLayoutAppearance
,否则,如果您在 iPhone 上使用横向模式,则您的自定义样式将不适用。

class CustomViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let tabBarAppearnace = UITabBarAppearance()
        let tabFont =  UIFont.boldSystemFont(ofSize: 18)
        
        let selectedAttributes: [NSAttributedString.Key: Any]
        = [NSAttributedString.Key.font: tabFont, NSAttributedString.Key.foregroundColor: UIColor.orange]
        let normalAttributes: [NSAttributedString.Key: Any]
        = [NSAttributedString.Key.font: tabFont, NSAttributedString.Key.foregroundColor: UIColor.black]
        
        tabBarAppearnace.stackedLayoutAppearance.normal.titleTextAttributes = normalAttributes
        tabBarAppearnace.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttributes
        
        //New        
        tabBarAppearnace.inlineLayoutAppearance.normal.titleTextAttributes = normalAttributes
        tabBarAppearnace.inlineLayoutAppearance.selected.titleTextAttributes = selectedAttributes

        tabBarAppearnace.compactInlineLayoutAppearance.normal.titleTextAttributes = normalAttributes
        tabBarAppearnace.compactInlineLayoutAppearance.selected.titleTextAttributes = selectedAttributes


        tabBar.standardAppearance = tabBarAppearnace
    }
}

了解更多信息:https://developer.apple.com/documentation/uikit/uitabbarappearance


1
投票

如果有人感兴趣,你也可以在 iOS 15、Xcode 13 的 Storyboard 中实现这一点:

  • 在故事板中,突出显示选项卡栏
  • 打开检查器(Xcode 窗口右上角的按钮)
  • 打开属性检查器(三个滑动条)
  • 在选项卡栏下,选中外观框:标准和/或滚动边缘

  • 然后向下滚动并找到标准布局外观
  • 将堆叠选项设置为自定义
  • 现在您应该看到标准堆叠布局外观属性
  • 对于设置为正常的状态配置,请确保设置标题和图标颜色。
  • 然后还将“状态”更改为“已选择”并设置标题和图标颜色。

现在我们需要配置iPad的内联布局

所以现在我们需要对内联布局做同样的事情,在属性检查器的同一部分中,您将内联属性,将该选项更改为自定义。并按照上述步骤进行设置。

我建议您也对紧凑内联布局执行相同的操作。

如果您使用情节提要而不是对其进行编码,那么您可能还需要考虑配置滚动边缘外观,您将必须重复我们刚刚为滚动边缘外观的标准外观所做的所有操作。


0
投票

这是ios15的bug,你会在Ipad中发现,只需设置tintColor即可解决这个问题

 if #available(iOS 15.0, *) {

     tabbar.tintColor = .orange
    
   } else {
    
    tabBar.barTintColor = .orange
    
   }

0
投票
       let appearance = self.tabBar.standardAppearance.copy()
        
        appearance.configureWithTransparentBackground()
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

        let normalAttributes: [NSAttributedString.Key: Any]
        = [NSAttributedString.Key.foregroundColor: Constant.AppColor.tabBarItemUnselectedColor]
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = normalAttributes
        appearance.inlineLayoutAppearance.normal.titleTextAttributes = normalAttributes
        appearance.compactInlineLayoutAppearance.normal.titleTextAttributes = normalAttributes

        tabBar.standardAppearance = appearance
© www.soinside.com 2019 - 2024. All rights reserved.