本文介绍了在Xcode11/iOS13或13.1中自定义UITabBar的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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:

It is supposed to appear like this, with the height of tab bar being 72:

但是使用Xcode 11在iOS 12中看起来像这样,选项卡栏的高度返回到默认值49:

However using Xcode 11 it looks like this in iOS 12, the tab bar height goes back to the default 49:

和在iOS 13中,即使我的应用程序仅设置为纵向布局且目标设备仅是iPhone,它的显示也类似于 .inlineLayoutAppearance .我的自定义字体也返回到系统默认字体.像在iOS 12中一样,UITabBar的高度返回到默认值49:

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. My customised font also goes back to the system default one, too. Like in iOS 12, the UITabBar height goes back to default 49:

我提到了这个类似的问题,但是该解决方案对我不起作用,而且看起来也不像是正确的解决方案.

I referred to this similar question but the solution doesn't work for me, and it doesn't look like a proper solution anyway.

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

Another thing I don't understand related to this is that, when I tried to set the UITabBarItem's appearance with the following code:

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外观的方法,这确实令人困惑...

I got an error saying that 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?... I couldn't figure out a way to set my UITabBarItem's appearance either, which is really confusing...

有人知道是否有任何合理的原因,或者这可能是一个错误?谢谢您的回答.

Does anyone know if there is any reasonable reason for this, or it's a possible bug here? Thanks ahead for any answer.

推荐答案

这是我最终解决标签栏高度问题的方法,但是标题的位置仍然无法解决,此刻我必须使用带有文字.

This is how I finally solve the tab bar height problem, however the position of title is still not solved, at the moment I have to use the icon image with text instead.

@IBDesignable class MyTabBar: UITabBar {

    let higherTabBarInset: CGFloat = 24

    lazy var isIphoneXOrHigher: Bool = {
        return UIDevice().userInterfaceIdiom == .phone && UIScreen.main.nativeBounds.height >= 2436
    }()

    lazy var TAB_BAR_HEIGHT: CGFloat = {
        // Return according to default tab bar height
        if GlobalData.isIphoneXOrHigher {
            return 83 + higherTabBarInset
        }
        else {
            return 49 + higherTabBarInset
        }
    }()

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        if #available(iOS 13.0, *) {
            self.standardAppearance.compactInlineLayoutAppearance = UITabBarItemAppearance.init(style: .stacked)
            self.standardAppearance.inlineLayoutAppearance = UITabBarItemAppearance.init(style: .stacked)
            self.standardAppearance.stackedLayoutAppearance = UITabBarItemAppearance.init(style: .stacked)
            self.standardAppearance.stackedItemPositioning = .centered
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    override func sizeThatFits(_ size: CGSize) -> CGSize {
        var size = super.sizeThatFits(size)
        size.height = TAB_BAR_HEIGHT
        return size
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        self.items?.forEach({ e in
            if #available(iOS 13.0, *) {
                e.standardAppearance = self.standardAppearance
            }
            else {
                e.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -(higherTabBarInset / 2))
            }
        })
    }
}

这篇关于在Xcode11/iOS13或13.1中自定义UITabBar的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 05:10