2

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

Nikita Zernov
  • 5,465
  • 6
  • 39
  • 70

1 Answers1

1

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

Alexey Bondarchuk
  • 2,002
  • 2
  • 20
  • 22