【问题标题】:UITabBarAppearance does not work on iOS15 iPad (title color)UITabBarAppearance 在 iOS15 iPad 上不起作用(标题颜色)
【发布时间】:2021-11-11 23:39:39
【问题描述】:

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

我想将 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


    【解决方案1】:

    对于 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

    【讨论】:

      猜你喜欢
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 2016-02-15
      • 2021-10-30
      • 2019-11-29
      • 2021-11-17
      相关资源
      最近更新 更多