gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 09:28:41 32 4
gpt4 key购买 nike

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

    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
}

enter image description here


但是当您在 UITabBar 外观代理上设置一个 UITabBarAppearance 时,当您选择标签时字体大小会被破坏:

    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
}

enter image description here



问题:

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


背景信息:

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

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

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

最佳答案

好吧,我想出了一个解决办法(感觉我现在只是在使用 Stack Overflow 来 rubber duck 😎)。

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

请注意,仍然有必要直接在 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
}

关于ios - 选择选项卡时,设置 UITabBarAppearance 会破坏 UITabBarItem 外观代理字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71194968/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com