改变UITabBar的色调/背景颜色

pix*_*xel 86 iphone uitabbarcontroller uitabbar ios

UINavigationBar和UISearchBar都有一个tintColor属性,允许你改变这两个项目的色调(我知道).我想在我的应用程序中对UITabBar做同样的事情,但现在已经找到了从默认的黑色中改变它的方法.有任何想法吗?

imn*_*mnk 104

iOS 5添加了一些新的外观方法,用于自定义大多数UI元素的外观.

您可以使用外观代理在应用程序中定位UITabBar的每个实例.

对于iOS 5 + 6:

[[UITabBar appearance] setTintColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)

对于iOS 7及更高版本,请使用以下内容:

[[UITabBar appearance] setBarTintColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)

使用外观代理将更改整个应用程序中的任何标签栏实例.对于特定实例,请使用该类的一个新属性:

UIColor *tintColor; // iOS 5+6
UIColor *barTintColor; // iOS 7+
UIColor *selectedImageTintColor;
UIImage *backgroundImage;
UIImage *selectionIndicatorImage;
Run Code Online (Sandbox Code Playgroud)

  • 不.正如我所指出的,它们适用于iOS 5.您始终可以使用条件编译语句来定位特定版本:http://cocoawithlove.com/2010/07/tips-tricks-for-conditional-ios3-ios32.html (3认同)

con*_*are 47

我已经能够通过子类化UITabBarController并使用私有类来使其工作:

@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
Run Code Online (Sandbox Code Playgroud)


小智 34

我有一个最终答案的附录.虽然基本方案是正确的,但可以改进使用部分透明颜色的技巧.我假设它只是让默认渐变显示出来.哦,同样,TabBar的高度是49像素而不是48像素,至少在OS 3中是这样.

因此,如果你有一个带有渐变的适当的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];

}
Run Code Online (Sandbox Code Playgroud)


Mar*_*ski 27

当您使用addSubview时,您的按钮将失去可点击性,因此不会

[[self tabBar] addSubview:v];
Run Code Online (Sandbox Code Playgroud)

使用:

[[self tabBar] insertSubview:v atIndex:0];
Run Code Online (Sandbox Code Playgroud)


Lou*_*arg 7

没有简单的方法可以做到这一点,你基本上需要子类UITabBar并实现自定义绘图来做你想要的.对于这种效果来说,这是相当多的工作,但它可能是值得的.我建议向Apple提交一个错误,将其添加到未来的iPhone SDK中.

  • 先生,你填的苹果虫子了吗? (4认同)

Gau*_*rav 7

以下是完美的解决方案.这适用于iOS5和iOS4.

//---- 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];
}
Run Code Online (Sandbox Code Playgroud)


cap*_*kaw 7

在iOS 7上:

[[UITabBar appearance] setBarTintColor:[UIColor colorWithRed:(38.0/255.0) green:(38.0/255.0) blue:(38.0/255.0) alpha:1.0]];
Run Code Online (Sandbox Code Playgroud)

我还建议先根据您的视觉需求进行设置:

[[UITabBar appearance] setBarStyle:UIBarStyleBlack];
Run Code Online (Sandbox Code Playgroud)

条形样式在视图内容和标签栏之间放置一个微妙的分隔符.


小智 5

[[self tabBar] insertSubview:v atIndex:0]; 适合我.


Muh*_*wan 5

对我来说,改变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"];
Run Code Online (Sandbox Code Playgroud)

试试这个!!!