iPadOS 15 UITabBar 标题被截断
iPadOS 15 UITabBar title cut off
自从我升级了我的 iPad 操作系统后,我的应用程序的 UITabBar 的标题显示被截断,如屏幕截图所示。
我尝试了一些方法,但没有找到正确的解决方法。
希望有人能帮助我。
这是代码:
func setupTabBar() {
if #available(iOS 13, *) {
let appearance = tabBar.standardAppearance
appearance.configureWithOpaqueBackground()
appearance.backgroundImage = UIImage(color: .white)
appearance.shadowImage = UIImage(color: .clear)
let normalAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.gray]
let selectedAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.red]
appearance.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.stackedLayoutAppearance.normal.titleTextAttributes = normalAttrs
appearance.inlineLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.inlineLayoutAppearance.normal.titleTextAttributes = normalAttrs
appearance.compactInlineLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.compactInlineLayoutAppearance.normal.titleTextAttributes = normalAttrs
UITabBar.appearance().standardAppearance = appearance
} else {
tabBar.backgroundImage = UIImage(color: .white)
tabBar.shadowImage = UIImage(color: .clear)
}
if #available(iOS 15, *) {
UITabBar.appearance().scrollEdgeAppearance = UITabBar.appearance().standardAppearance
}
}
出于某种原因,设置 titleTextAttributes
似乎是导致 inlineLayoutAppearance
出现问题的原因,包括 NSParagraphStyle.default
的默认段落样式可以解决此问题。
对于您的代码,以下更改应该可以修复它(从 iOS 15.0 开始)。
let normalAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.gray, .paragraphStyle: NSParagraphStyle.default]
let selectedAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.red, .paragraphStyle: NSParagraphStyle.default]
对于那些不能有默认段落样式的人,为大多数州设置属性得到 OS 为我正确调整标签大小。
Swift 5.0:
UITabBarItem.appearance()
.setTitleTextAttributes(
customAttributesWithCustomParagraphStyle,
for: [
.normal,
.highlighted,
.disabled,
.selected,
.focused,
.application,
]
)
如果不设置至少 .normal
和 .selected
,UITabBarItem 标题在与自定义属性一起使用时会被截断。
自从我升级了我的 iPad 操作系统后,我的应用程序的 UITabBar 的标题显示被截断,如屏幕截图所示。
我尝试了一些方法,但没有找到正确的解决方法。
希望有人能帮助我。
这是代码:
func setupTabBar() {
if #available(iOS 13, *) {
let appearance = tabBar.standardAppearance
appearance.configureWithOpaqueBackground()
appearance.backgroundImage = UIImage(color: .white)
appearance.shadowImage = UIImage(color: .clear)
let normalAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.gray]
let selectedAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.red]
appearance.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.stackedLayoutAppearance.normal.titleTextAttributes = normalAttrs
appearance.inlineLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.inlineLayoutAppearance.normal.titleTextAttributes = normalAttrs
appearance.compactInlineLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.compactInlineLayoutAppearance.normal.titleTextAttributes = normalAttrs
UITabBar.appearance().standardAppearance = appearance
} else {
tabBar.backgroundImage = UIImage(color: .white)
tabBar.shadowImage = UIImage(color: .clear)
}
if #available(iOS 15, *) {
UITabBar.appearance().scrollEdgeAppearance = UITabBar.appearance().standardAppearance
}
}
出于某种原因,设置 titleTextAttributes
似乎是导致 inlineLayoutAppearance
出现问题的原因,包括 NSParagraphStyle.default
的默认段落样式可以解决此问题。
对于您的代码,以下更改应该可以修复它(从 iOS 15.0 开始)。
let normalAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.gray, .paragraphStyle: NSParagraphStyle.default]
let selectedAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.red, .paragraphStyle: NSParagraphStyle.default]
对于那些不能有默认段落样式的人,为大多数州设置属性得到 OS 为我正确调整标签大小。
Swift 5.0:
UITabBarItem.appearance()
.setTitleTextAttributes(
customAttributesWithCustomParagraphStyle,
for: [
.normal,
.highlighted,
.disabled,
.selected,
.focused,
.application,
]
)
如果不设置至少 .normal
和 .selected
,UITabBarItem 标题在与自定义属性一起使用时会被截断。