简体   繁体   English

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

[英]Customise UITabBar height in Xcode11 / iOS13 or 13.1

I used to use the following code to adjust the height of my tab bar.我曾经使用以下代码来调整标签栏的高度。 However after I upgrade to Xcode 11 and using swift 5, the UI doesn't appear correctly anymore.但是,在我升级到 Xcode 11 并使用 swift 5 后,用户界面不再正确显示。

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))
        })
    }
}

It is supposed to appear like this, with the height of tab bar being 72:它应该看起来像这样,标签栏的高度为 72:

预期显示

However using Xcode 11 it looks like this in iOS 12, the tab bar height goes back to the default 49:但是,使用 Xcode 11 在 iOS 12 中看起来像这样,标签栏高度回到默认值 49:

ios 12下显示异常

and in iOS 13, it displays like in .inlineLayoutAppearance even if my app was set for portrait layout only and the target device is iPhone only.在 iOS 13 中,即使我的应用程序仅设置为纵向布局并且目标设备仅是 iPhone,它也会像在.inlineLayoutAppearance中一样显示。 My customised font also goes back to the system default one, too.我的自定义字体也可以回到系统默认字体。 Like in iOS 12, the UITabBar height goes back to default 49:就像在 iOS 12 中一样,UITabBar 高度回到默认值 49:

ios 13下显示异常

I referred to this similar question but the solution doesn't work for me, and it doesn't look like a proper solution anyway.我提到了这个类似的问题,但该解决方案对我不起作用,而且它看起来也不是一个合适的解决方案。

Another thing I don't understand related to this is that, when I tried to set the UITabBarItem's appearance with the following code:与此相关的另一件事我不明白的是,当我尝试使用以下代码设置 UITabBarItem 的外观时:

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

I got an error saying that Cannot assign value of type 'UITabBarItemAppearance' to type 'UITabBarAppearance?我收到一条错误消息,提示Cannot assign value of type 'UITabBarItemAppearance' to type 'UITabBarAppearance? . . Then I found that even if the type of my iteration variable e is UITabBarItem , the type of its appearance is UITabBarAppearance?然后我发现即使我的迭代变量e的类型是UITabBarItem ,它的外观类型是UITabBarAppearance? ... I couldn't figure out a way to set my UITabBarItem's appearance either, which is really confusing... ...我也想不出一种方法来设置我的 UITabBarItem 的外观,这真的很令人困惑...

Does anyone know if there is any reasonable reason for this, or it's a possible bug here?有谁知道这是否有任何合理的原因,或者这可能是一个错误? Thanks ahead for any answer.提前感谢您的任何回答。

Setting different tab bar height seems to work for me when a new frame is set with viewDidLayoutSubviews() instead of viewWillLayoutSubviews()当使用viewDidLayoutSubviews()而不是viewWillLayoutSubviews()设置新框架时,设置不同的标签栏高度似乎对我有用

As for setting text offset please try至于设置文本偏移量,请尝试

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

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

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