How to correct Tab Bar height issue on iPhone X

前端 未结 20 3600
长发绾君心
长发绾君心 2020-12-13 13:07

I\'m having an issue with my app when testing for iPhone X. I\'m not sure how to adjust this issue, as well as not make it an issue for non iPhone X sizes. This only seems t

20条回答
  •  余生分开走
    2020-12-13 13:54

    Add this code in viewDidLoad

    DispatchQueue.main.async {
                let size = CGSize(width: self.tabBar.frame.width / numberOfTabsFloat,
                                  height: self.tabBar.frame.height)
                let image = UIImage.drawTabBarIndicator(color: UIColor.white,
                                                       size: size,
                                                       onTop: false)
                UITabBar.appearance().selectionIndicatorImage = image
                self.tabBar.selectionIndicatorImage = image
            }
    

    and add this extension

    extension UIImage{
        //Draws the top indicator by making image with filling color
        class func drawTabBarIndicator(color: UIColor, size: CGSize, onTop: Bool) -> UIImage {
            let indicatorHeight = size.height
            let yPosition = onTop ? 0 : (size.height - indicatorHeight)
    
            UIGraphicsBeginImageContextWithOptions(size, false, 0)
            color.setFill()
            UIRectFill(CGRect(x: 0, y: yPosition, width: size.width, height: indicatorHeight))
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            return image!
        }
    }
    

提交回复
热议问题