Add a line as a selection indicator to a UITabbarItem in Swift

后端 未结 4 1554
走了就别回头了
走了就别回头了 2020-12-09 19:18

I wanna use a thick line at the bottom of a UITabbarItems as a selection indicator. Due to the fact that the App must work on different phone sizes, I cannot use a image as

4条回答
  •  青春惊慌失措
    2020-12-09 19:58

    Swift 4.x
    Xcode 10.x

    extension UIImage {
        func createSelectionIndicator(color: UIColor, size: CGSize, lineWidth: CGFloat) -> UIImage {
            UIGraphicsBeginImageContextWithOptions(size, false, 0)
            color.setFill()
            UIRectFill(CGRect(x: 0, y: size.height - lineWidth, width: size.width, height: lineWidth))
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image!
        }
    }
    

    Use

    tabBar.selectionIndicatorImage = UIImage().createSelectionIndicator(color: BLUE, size: CGSize(width: tabBar.frame.width/CGFloat(tabBar.items!.count), height:  tabBar.frame.height), lineWidth: 2.0)
    

提交回复
热议问题