在IT技术中,UINavigationBar和UISearchBar都有一个tintColor属性,允许您更改这些项目的色调颜色(是的,很惊讶)。我希望在我的应用程序中对UITabBar执行相同的操作,但是发现无法将其从默认的黑色更改。有任何想法吗?
在IT技术中,UINavigationBar和UISearchBar都有一个tintColor属性,允许您更改这些项目的色调颜色(是的,很惊讶)。我希望在我的应用程序中对UITabBar执行相同的操作,但是发现无法将其从默认的黑色更改。有任何想法吗?
只为背景色
Tabbarcontroller.tabBar.barTintColor=[UIColor redcolour];
[[UITabBar appearance] setBackgroundColor:[UIColor blackColor]];
更改选项卡栏未选中图标颜色
iOS 10:
// this code need to be placed on home page of tabbar
for(UITabBarItem *item in self.tabBarController.tabBar.items) {
item.image = [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
iOS 10及以上版本:
// this need to be in appdelegate didFinishLaunchingWithOptions
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blackColor]];
[v setBackgroundColor ColorwithRed: Green: Blue: ];
现有答案中有一些好的想法,它们的工作方式略有不同,您的选择也将取决于您所针对的设备以及您希望实现的外观类型。在自定义外观方面,UITabBar
非常不直观,但以下几个技巧可能会有所帮助:
1). 如果您想要摆脱光泽叠加效果,使其看起来更平整,请执行以下操作:
tabBar.backgroundColor = [UIColor darkGrayColor]; // this will be your background
[tabBar.subviews[0] removeFromSuperview]; // this gets rid of gloss
2). 要为 tabBar 按钮设置自定义图片,请执行以下操作:
for (UITabBarItem *item in tabBar.items){
[item setFinishedSelectedImage:selected withFinishedUnselectedImage:unselected];
[item setImageInsets:UIEdgeInsetsMake(6, 0, -6, 0)];
}
当你需要选择一个UIImage
对象时,可以使用selected
或unselected
。如果你想要它们成为纯色,最简单的方法是创建一个具有所需backgroundColor
的UIView
,然后借助QuartzCore将其呈现为UIImage
。我在UIView
的类别中使用以下方法来获取视图内容的UIImage
:
- (UIImage *)getImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [[UIScreen mainScreen]scale]);
[[self layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
3) 最后,您可能希望自定义按钮标题的样式。请执行以下操作:
for (UITabBarItem *item in tabBar.items){
[item setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], UITextAttributeTextColor,
[UIColor whiteColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont boldSystemFontOfSize:18], UITextAttributeFont,
nil] forState:UIControlStateNormal];
}
UITextAttributeTextColor
设置为透明,并将你的文本添加到第二部分的 selected
和 unselected
图像中。你好,我正在使用iOS SDK 4,只需两行代码即可解决此问题,代码如下:
tBar.backgroundColor = [UIColor clearColor];
tBar.backgroundImage = [UIImage imageNamed:@"your-png-image.png"];
if ([tabBar respondsToSelector:@selector(setBackgroundImage:)]) {
// ios 5 code here
[tabBar setBackgroundImage:[UIImage imageNamed:@"image.png"]];
}
else {
// ios 4 code here
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *tabbg_view = [[UIView alloc] initWithFrame:frame];
UIImage *tabbag_image = [UIImage imageNamed:@"image.png"];
UIColor *tabbg_color = [[UIColor alloc] initWithPatternImage:tabbag_image];
tabbg_view.backgroundColor = tabbg_color;
[tabBar insertSubview:tabbg_view atIndex:0];
}
如果你想在 Swift 3 中使用 AppDelegate
来设置外观,可以按照以下步骤:
UITabBar.appearance().barTintColor = your_color
Swift 3.0答案:(来自Vaibhav Gaikwad)
更改选项卡栏未选择图标的颜色:
if #available(iOS 10.0, *) {
UITabBar.appearance().unselectedItemTintColor = UIColor.white
} else {
// Fallback on earlier versions
for item in self.tabBar.items! {
item.image = item.image?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
}
}
仅更改文本颜色:
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red, for: .selected)