UITabBarItem - iOS 9 中的背景颜色

UITabBarItem - Background color in iOS 9

我正在尝试以这种方式更改 UITabBarItem 的背景颜色:

UITabBar.appearance().selectionIndicatorImage = UIImage.imageWithColor(UIColor.blackColor())

UIImageextension

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

它不起作用。最终应该是这样的:

看起来你的图片太小了let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)。尝试替换为 let rect = CGRectMake(0.0, 0.0, 50.0, 50.0)