I am trying to change background color of my UITabBarItem this way:
UITabBar.appearance().selectionIndicatorImage = UIImage.imageWithColor(UIColor.blackColor())
Here's extension
for UIImage
:
extension UIImage {
class func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Looks like your image is too small let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
. Try to replace with let rect = CGRectMake(0.0, 0.0, 50.0, 50.0)
.