gpt4 book ai didi

ios - 在Xcode11/iOS13或13.1中自定义UITabBar高度

转载 作者:行者123 更新时间:2023-12-02 01:08:55 38 4
gpt4 key购买 nike

我曾经使用以下代码来调整标签栏的高度。但是,在我升级到 Xcode 11 并使用 swift 5 后,UI 不再正确显示。

class MyTabBarController: UITabBarController {

private lazy var defaultTabBarHeight = { [unowned self] in
return self.tabBar.frame.size.height
}()

let higherTabBarInset: CGFloat = 24
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

let newTabBarHeight = defaultTabBarHeight + higherTabBarInset
var newFrame = tabBar.frame
newFrame.size.height = newTabBarHeight
newFrame.origin.y = view.frame.size.height - newTabBarHeight
tabBar.items?.forEach({e in
e.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -(higherTabBarInset / 2))
})
}
}

它应该看起来像这样,标签栏的高度为72:

expected display

但是使用 Xcode 11,它在 iOS 12 中看起来像这样,标签栏高度回到默认的 49:

abnormal display under ios 12

在 iOS 13 中,即使我的应用程序设置为仅纵向布局并且目标设备仅为 iPhone,它也会像 .inlineLayoutAppearance 一样显示。我的自定义字体也恢复为系统默认字体。与 iOS 12 一样,UITabBar 高度恢复为默认值 49:

abnormal display under ios 13

我提到了this similar question但该解决方案对我不起作用,而且无论如何它看起来都不是一个合适的解决方案。

与此相关的另一件事我不明白的是,当我尝试使用以下代码设置 UITabBarItem 的外观时:

tabBar.items?.forEach({e in
if #available(iOS 13.0, *) {
let appearance = UITabBarItemAppearance()
appearance.configureWithDefault(for: .stacked)
e.standardAppearance = appearance
}
})

我收到一条错误消息,指出无法将“UITabBarItemAppearance”类型的值分配给“UITabBarAppearance”类型?。然后我发现即使我的迭代变量e的类型是UITabBarItem,它的外观类型是UITabBarAppearance?...我不能也没有找到设置 UITabBarItem 外观的方法,这真的很令人困惑......

有谁知道这是否有任何合理的原因,或者这里可能存在错误?在此先感谢您的回答。

最佳答案

当使用 viewDidLayoutSubviews() 而不是 viewWillLayoutSubviews() 设置新框架时,设置不同的选项卡栏高度似乎对我有用

至于设置文本偏移量请尝试

if #available(iOS 13, *) {
let appearance = self.tabBar.standardAppearance.copy()
appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -4)
tabBar.standardAppearance = appearance
}

关于ios - 在Xcode11/iOS13或13.1中自定义UITabBar高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58091096/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com