[英]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]
對於那些不能有默認段落樣式的人,為大多數州設置屬性可以讓操作系統為我正確調整標簽大小。
斯威夫特 5.0:
UITabBarItem.appearance()
.setTitleTextAttributes(
customAttributesWithCustomParagraphStyle,
for: [
.normal,
.highlighted,
.disabled,
.selected,
.focused,
.application,
]
)
如果不將其設置為至少.normal
和.selected
,則 UITabBarItem 標題在與自定義屬性一起使用時會被截斷。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.