选择选项卡时,设置 UITabBarAppearance 会破坏 UITabBarItem 外观代理字体大小

Jon*_*Cox 4 uikit uitabbar ios uiappearance swift

以下内容将标题文本外观代理设置为使用大字体,按预期工作,并且UITabBarItem当您选择它们​​时, s 会保留其大字体:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        // UITabBar.appearance().standardAppearance = UITabBarAppearance()
                
        let bigFont = UIFont.systemFont(ofSize: 20)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: bigFont], for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: bigFont], for: .selected)
        
        return true
    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


UITabBarAppearance但是,当您在外观代理上设置 a 时UITabBar,选择选项卡时字体大小会被破坏:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        UITabBar.appearance().standardAppearance = UITabBarAppearance()
                
        let bigFont = UIFont.systemFont(ofSize: 20)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: bigFont], for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: bigFont], for: .selected)
        
        return true
    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述



问题:

有谁知道为什么设置 aUITabBarAppearance会破坏setTitleTextAttributes字体大小选择?


背景资料:

standardAppearance我使用设置的原因UITabBar是因为在我的实际项目中,我使用以下代码来保留选项卡栏的旧外观,而不是当屏幕上没有内容时变为透明的新外观(并搞乱了应用程序设计)。

如果有一种方法可以在不破坏字体大小的情况下实现这一点,那么这可能是一个潜在的“修复”:)

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

Jon*_*Cox 11

好吧,我找到了一个解决方案(有点感觉我此时只是使用 Stack Overflow 来橡皮鸭)。

以下将在选择选项卡后保留正确的字体大小。

UITabBarItem请注意,仍然需要直接在(线条)上设置属性UITabBarItem.appearance().setTitleTextAttributes...,因为否则选项卡将不会以正确的字体大小开始,它们只会在用户选择后才会更改为正确的字体大小。

另请注意,如果您在选项卡栏上设置standardAppearance,您可能还需要设置scrollEdgeAppearance- 就像我在原始问题的最后一个代码片段中所做的那样。

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let bigFont = UIFont.systemFont(ofSize: 20)
        let textAttributes = [NSAttributedString.Key.font: bigFont]
        
        UITabBarItem.appearance().setTitleTextAttributes(textAttributes, for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes(textAttributes, for: .selected)
        
        let tabBarItemAppearance = UITabBarItemAppearance()
        tabBarItemAppearance.normal.titleTextAttributes = textAttributes
        tabBarItemAppearance.selected.titleTextAttributes = textAttributes
        
        let tabBarAppearance = UITabBarAppearance()
        tabBarAppearance.inlineLayoutAppearance = tabBarItemAppearance
        tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance
        tabBarAppearance.compactInlineLayoutAppearance = tabBarItemAppearance
        
        UITabBar.appearance().standardAppearance = tabBarAppearance
        
        return true
    }
Run Code Online (Sandbox Code Playgroud)