Search code examples
iosswiftuitabbaruitabbaritemios9

UITabBarItem - Background color in iOS 9


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
    }
}

It does not work. Here's how it should look finally:enter image description here


Solution

  • 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).