答案 0 :(得分:0)
默认标签色调颜色: -
Obj-C版本: -
[[self tabBar] setTintColor:[UIColor blackColor]];
Swift 2.2版本: -
self.tabBar.tintColor = UIColor.whiteColor()
所选标签色调颜色: -
Obj-C版本: -
[[self tabBar] setSelectedImageTintColor:[UIColor blueColor]];
Swift2.2版本: -
self.tabBar.selectedImageTintColor = UIColor.blueColor()
答案 1 :(得分:0)
我发现解决方案是使用此循环
for(UITabBarItem *item in self.tabBar.items) {
// use the UIImage category code for the imageWithColor: method
item.image = [[[item selectedImage] imageWithColor:[UIColor redColor]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
在UITabBarController
课程中。
并应添加包含方法UIImage+Overlay
imageWithColor
- (UIImage *)imageWithColor:(UIColor *)color1
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextClipToMask(context, rect, self.CGImage);
[color1 setFill];
CGContextFillRect(context, rect);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
答案 2 :(得分:0)
我找到了this link的另一个解决方案。 没有必要使用循环。我修改了他的代码以删除警告
makeImageWithColorAndSize函数可以实现为普通函数
extension UIImage {
func makeImageWithColorAndSize(color: UIColor, size: CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(CGRectMake(0, 0, size.width, size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
使用示例:
override func viewWillAppear(animated: Bool) {
self.tabBar.selectionIndicatorImage = UIImage().makeImageWithColorAndSize(
UIColor.blueColor(),
size: CGSizeMake(tabBar.frame.width/5, tabBar.frame.height))
super.viewWillAppear(animated)
}
原始代码Gwendle