Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
uitabaritem-iOS 9中的背景色_Ios_Swift_Uitabbar_Uitabbaritem_Ios9 - Fatal编程技术网

uitabaritem-iOS 9中的背景色

uitabaritem-iOS 9中的背景色,ios,swift,uitabbar,uitabbaritem,ios9,Ios,Swift,Uitabbar,Uitabbaritem,Ios9,我正试图通过以下方式更改UITabBarItem的背景色: UITabBar.appearance().selectionIndicatorImage = UIImage.imageWithColor(UIColor.blackColor()) 以下是UIImage的扩展: extension UIImage { class func imageWithColor(color: UIColor) -> UIImage { let rect = CGRectMake

我正试图通过以下方式更改UITabBarItem的背景色:

UITabBar.appearance().selectionIndicatorImage = UIImage.imageWithColor(UIColor.blackColor())
以下是
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
    }
}

它不起作用。下面是它的最终外观:

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

为什么不使用uitabar.appearance().tintColor属性呢?我正在将“淡色”设置为白色,需要将“背景色”设置为黑色。查看更新的问题。@NikitaZernov关于我的答案有任何更新吗?