根据屏幕大小设置不同的 TabBar 高度 Swift

时间:2021-06-01 17:30:43

标签: ios swift

我正在使用 ESTabBarController 在我的应用程序中创建自定义标签栏。但是,当我设置它时,文本要么不显示,要么被向上滑动主页栏覆盖。我尝试使用 if 来检查屏幕尺寸是否为 == 屏幕尺寸之一,但它仍然无法正确显示。

enter image description here

override func viewWillLayoutSubviews() {
    // Set different tabBar height for Phone X
    var heightOffset: CGFloat = 5
    if UIDevice.current.userInterfaceIdiom == .phone && (UIScreen.main.nativeBounds.height == 2436 || UIScreen.main.nativeBounds.height == 1792 || UIScreen.main.nativeBounds.height == 2688) {
        heightTabBar = CGFloat(60)
        heightOffset = 20
    }

    tabBar.frame = CGRect(
        origin: CGPoint(x: 0, y: view.frame.size.height - heightTabBar - heightOffset),
        size: CGSize(width: UIScreen.main.bounds.width, height: heightTabBar)
    )
    
    tabBar.barTintColor = ThemeManager.viewGradientColour2
    //add line in itemBar
    bottomLineViewItemBar.frame = CGRect(x: 0, y: tabBar.bounds.height, width: tabBar.bounds.width, height: heightOffset)
    bottomLineViewItemBar.backgroundColor = ThemeManager.viewGradientColour2
    tabBar.addSubview(bottomLineViewItemBar)
}

1 个答案:

答案 0 :(得分:0)

首先,我会以不同的方式使用 var height = UIScreen.main.bounds.size.height。要分配 TabBar 的高度,我将使用 heightTabBar = CGFloat(0.1*height)。这意味着 TabBar 将是屏幕高度的 10%(您可以更改该值)。

此外,您可以运行 safe_bottom = UIApplication.shared.windows[0].safeAreaInsets.bottom,以避免字母被向上滑动的主页栏重叠。 safe_bottom 具有从屏幕底部到用户与您的 UI 交互的屏幕部分的距离。因此,您只需将 TabBar 放置在距屏幕底部 safe_bottom 的高度(以像素为单位)。

相关问题