小编exo*_*tue的帖子

UITabBarItem选项卡栏项目单个选定的背景颜色

在我的项目中,我使用UIImage的扩展来创建更改选项卡栏项的选定背景颜色:

extension UIImage {
func imageWithColor(tintColor: UIColor) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)

    let context = UIGraphicsGetCurrentContext() as CGContextRef!
    CGContextTranslateCTM(context, 0, self.size.height)
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, CGBlendMode.Normal)

    let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
    CGContextClipToMask(context, rect, self.CGImage)
    tintColor.setFill()
    CGContextFillRect(context, rect)

    let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
    UIGraphicsEndImageContext()

    return newImage
}

func makeImageWithColorAndSize(color: UIColor, size: CGSize) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    color.setFill()
    UIRectFill(CGRectMake(0, 0, 100, 100))
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}   
}
Run Code Online (Sandbox Code Playgroud)

然后在我的MainTabBarController中使用它

 class MainTabBarController: …
Run Code Online (Sandbox Code Playgroud)

xcode uitabbarcontroller swift xcode7 swift2

4
推荐指数
3
解决办法
8022
查看次数

标签 统计

swift ×1

swift2 ×1

uitabbarcontroller ×1

xcode ×1

xcode7 ×1