美好的一天, 我使用故事板中的UITabBarController,并且我想将图标颜色从默认的灰色更改为白色。 更改UITabBarController选项卡未选中时选项卡图标颜色
我尝试了很多解决方案,在这里找到stackOverflow,并在其他网站,但都没用。
美好的一天, 我使用故事板中的UITabBarController,并且我想将图标颜色从默认的灰色更改为白色。 更改UITabBarController选项卡未选中时选项卡图标颜色
我尝试了很多解决方案,在这里找到stackOverflow,并在其他网站,但都没用。
默认的标签着色颜色: -
的OBJ-C版本: -
[[self tabBar] setTintColor:[UIColor blackColor]];
雨燕2.2版本: -
self.tabBar.tintColor = UIColor.whiteColor()
选定的选项卡色调颜色: -
Obj-C Ve rsion: -
[[self tabBar] setSelectedImageTintColor:[UIColor blueColor]];
Swift2.2版本: -
默认tint颜色代码没有效果,第二个代码,为选定的标签颜色效果很好,但该功能已弃用 –
我找到了解决办法则是在UITabBarController
类使用这个循环
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];
}
。 并应添加含有方法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;
}
我找到了另一种解决方案从this link的UIImage+Overlay
。 没有必要使用循环。我修改了他的代码删除警告
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
代码你请给我提供你从哪里挑选你的链接p示例代码 –
请参阅此链接:http://stackoverflow.com/questions/790746/how-to-change-uitabbar-selection-color – MShah
当我再次搜索链接以获取示例代码时,我发现解决方案,谢谢大家 解决方案:http://stackoverflow.com/questions/30664500/change-default-not-selected-uitabbaritem-image-color –