Change only one specific UITabBarItem tint color

前端 未结 6 2263
猫巷女王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:18

    swift xcode7.1 tested :

    extension UIImage {
        func tabBarImageWithCustomTint(tintColor: UIColor) -> UIImage {
            UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
            let context: CGContextRef = UIGraphicsGetCurrentContext()!
    
            CGContextTranslateCTM(context, 0, self.size.height)
            CGContextScaleCTM(context, 1.0, -1.0)
            CGContextSetBlendMode(context, CGBlendMode.Normal)
            let rect: CGRect = CGRectMake(0, 0, self.size.width, self.size.height)
    
            CGContextClipToMask(context, rect, self.CGImage)
    
            tintColor.setFill()
            CGContextFillRect(context, rect)
    
            var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            newImage = newImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
            return newImage
        }
    }
    

    fixed the compatibility bug in @Binsh answer

提交回复
热议问题