ios iPadOS 15 UITabBar标题被切断

vi4fp9gy  于 2023-06-07  发布在  iOS
关注(0)|答案(3)|浏览(161)

由于我升级了我的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
    }
}

tf7tbtn2

tf7tbtn21#

出于某种原因,似乎设置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]
wsewodh2

wsewodh22#

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

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

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

6yt4nkrj

6yt4nkrj3#

今天我又花了一个小时在这个问题上挣扎。似乎是自定义字体给我带来了问题:

NSDictionary *attributesForNomalState = @{
    NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:UIFont.systemFontSize],
    NSForegroundColorAttributeName: [self colorForName:tabBarItemColor]
};

但是,我注意到,如果我像这样分配一个常量字符串,它可以正常工作:

tabItem.title = @"Test1";

但是,当我使用本地化字符串时,标题被截断了!如果我使用stringWithFormat函数在本地化的字符串后附加一个空格,它也对我有效。疯狂。如果有人知道为什么这个丑陋的变通办法的工作,我会有兴趣在您的评论!

相关问题