简体   繁体   中英

Setting different heights of TabBar depending on screen size Swift

I am using ESTabBarController to create a custom tab bar in my app. However when I set it the text is either not displaying or else it is getting covered by the Swipe up home bar. I tried using an if to check if the screen size was == to one of the screen sizes but it is still not displaying correctly.

在此处输入图像描述

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

First, I would use var height = UIScreen.main.bounds.size.height in a different way. To assign the height of the TabBar I would use heightTabBar = CGFloat(0.1*height) . This means that the TabBar will be 10% of the screen height (you can change the value).

Also, you can run safe_bottom = UIApplication.shared.windows[0].safeAreaInsets.bottom , to avoid the letters being overlapped by the swipe up home bar. safe_bottom has the distance from the bottom of the screen to the part of the screen where user interacts with your UI. So, you just have to place the TabBar at a height (in pixels) of safe_bottom from the bottom of the screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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