选择选项卡时,设置 UITabBarAppearance 会破坏 UITabBarItem 外观代理字体大小
Setting a UITabBarAppearance breaks UITabBarItem appearance proxy font size when selecting tabs
以下将标题文本外观代理设置为使用大字体,按预期工作,并且 UITabBarItem
在您 select 时保留其大字体:
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
}
但是当你在 UITabBar
外观代理上设置 UITabBarAppearance
时,当你 select 标签时字体大小被破坏:
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
}
问题:
有谁知道为什么设置 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
}
以下将标题文本外观代理设置为使用大字体,按预期工作,并且 UITabBarItem
在您 select 时保留其大字体:
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
}
但是当你在 UITabBar
外观代理上设置 UITabBarAppearance
时,当你 select 标签时字体大小被破坏:
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
}
问题:
有谁知道为什么设置 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
}