Change only one specific UITabBarItem tint color

前端 未结 6 2266
猫巷女王i
猫巷女王i 2021-01-05 00:53

It is well known that the tint color of selected (or active) items in a UITabBarController can be easily changed, here is an example:

myBarController.tabBar.         


        
6条回答
  •  清歌不尽
    2021-01-05 01:35

    Swift 5:

    extension UIImage {
        func tintWithColor(color: UIColor) -> UIImage? {
            UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
            guard let context = UIGraphicsGetCurrentContext() else { return nil }
            
            context.scaleBy(x: 1.0, y: -1.0)
            context.translateBy(x: 0.0, y: -self.size.height)
            context.setBlendMode(.multiply)
            
            let rect = CGRect(origin: .zero, size: size)
            guard let cgImage = self.cgImage else { return nil }
            context.clip(to: rect, mask: cgImage)
            color.setFill()
            context.fill(rect)
            
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            
            return newImage
            
        }
        
    }
    

提交回复
热议问题