在IT技术中,UINavigationBar和UISearchBar都有一个tintColor属性,允许您更改这些项目的色调颜色(是的,很惊讶)。我希望在我的应用程序中对UITabBar执行相同的操作,但是发现无法将其从默认的黑色更改。有任何想法吗?
在IT技术中,UINavigationBar和UISearchBar都有一个tintColor属性,允许您更改这些项目的色调颜色(是的,很惊讶)。我希望在我的应用程序中对UITabBar执行相同的操作,但是发现无法将其从默认的黑色更改。有任何想法吗?
iOS 5新增了一些外观方法,用于自定义大多数UI元素的外观。
您可以使用外观代理针对应用程序中的每个UITabBar实例。
iOS 5 + 6:
[[UITabBar appearance] setTintColor:[UIColor redColor]];
请使用以下内容来适用于 iOS 7 及以上版本:
[[UITabBar appearance] setBarTintColor:[UIColor redColor]];
使用外观代理将更改应用程序中的任何选项卡栏实例。要针对特定实例进行更改,请使用该类上的新属性之一:
UIColor *tintColor; // iOS 5+6
UIColor *barTintColor; // iOS 7+
UIColor *selectedImageTintColor;
UIImage *backgroundImage;
UIImage *selectionIndicatorImage;
@interface UITabBarController (private)
- (UITabBar *)tabBar;
@end
@implementation CustomUITabBarController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:kMainColor];
[v setAlpha:0.5];
[[self tabBar] addSubview:v];
[v release];
}
@end
我对最终答案提出了一点补充。 虽然基本方案是正确的,但使用部分透明颜色的技巧可以改进。 我假设它仅用于让默认梯度显示出来。 哦,还有,TabBar的高度至少在OS 3中为49像素,而不是48像素。
因此,如果您有一个适当的带渐变的1 x 49图片,这是您应该使用的viewDidLoad版本:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *v = [[UIView alloc] initWithFrame:frame];
UIImage *i = [UIImage imageNamed:@"GO-21-TabBarColorx49.png"];
UIColor *c = [[UIColor alloc] initWithPatternImage:i];
v.backgroundColor = c;
[c release];
[[self tabBar] addSubview:v];
[v release];
}
当您仅使用addSubview时,您的按钮将失去可点击性,因此请改用
[[self tabBar] addSubview:v];
使用:
[[self tabBar] insertSubview:v atIndex:0];
//---- For providing background image to tabbar
UITabBar *tabBar = [tabBarController tabBar];
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];
}
在iOS 7上:
[[UITabBar appearance] setBarTintColor:[UIColor colorWithRed:(38.0/255.0) green:(38.0/255.0) blue:(38.0/255.0) alpha:1.0]];
[[UITabBar appearance] setBarStyle:UIBarStyleBlack];
条形样式在您的视图内容和选项卡栏之间添加了一个微妙的分隔符。
要实现这个功能并没有简单的方法,你需要继承UITabBar并实现自定义绘制来达到你的目的。这需要相当多的工作,但是它可能是值得的。我建议向苹果提交一个bug,以便将其添加到未来的iPhone SDK中。
[[self tabBar] insertSubview:v atIndex:0];
对于我来说,像这样改变 Tabbar 的颜色非常简单:
[self.TabBarController.tabBar setTintColor:[UIColor colorWithRed:0.1294 green:0.5686 blue:0.8353 alpha:1.0]];
[self.TabBarController.tabBar setTintColor:[UIColor "YOUR COLOR"];
试试这个!!!
[[UITabBar appearance] setTintColor:[UIColor redColor]];
[[UITabBar appearance] setBarTintColor:[UIColor yellowColor]];