【问题标题】:iPadOS 15 UITabBar title cut offiPadOS 15 UITabBar 标题被截断
【发布时间】:2021-11-17 22:41:00
【问题描述】:

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

【问题讨论】:

  • 代码在哪里?

标签: ios ipados


【解决方案1】:

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

【讨论】:

  • 谢谢!救了我的命! (iOS 15 上的 iPad 应用)
  • 这并没有完全解决我的问题。设置默认段落样式会停止添加省略号,但文本仍会被剪裁,就好像标签框架不够大一样。这是在 iOS 15 上的 iPhone 应用上使用自定义字体。
【解决方案2】:

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

斯威夫特 5.0:

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

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

【讨论】:

    猜你喜欢
    • 2021-11-15
    • 2011-09-07
    • 2013-08-20
    • 2016-07-28
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多