繁体   English   中英

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]

对于那些不能有默认段落样式的人,为大多数州设置属性可以让操作系统为我正确调整标签大小。

斯威夫特 5.0:

    UITabBarItem.appearance()
        .setTitleTextAttributes(
            customAttributesWithCustomParagraphStyle,
            for: [
                .normal,
                .highlighted,
                .disabled,
                .selected,
                .focused,
                .application,
            ]
        )

如果不将其设置为至少.normal.selected ,则 UITabBarItem 标题在与自定义属性一起使用时会被截断。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM