ios 更改 UITabBar 的色调/背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/571028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:39:22  来源:igfitidea点击:

Changing Tint / Background color of UITabBar

iosiphoneuitabbarcontrolleruitabbar

提问by pixel

The UINavigationBar and UISearchBar both have a tintColor property that allows you to change the tint color (surprising, I know) of both of those items. I want to do the same thing to the UITabBar in my application, but have found now way to change it from the default black color. Any ideas?

UINavigationBar 和 UISearchBar 都有一个 tintColor 属性,允许您更改这两个项目的色调颜色(令人惊讶,我知道)。我想对我的应用程序中的 UITabBar 做同样的事情,但现在找到了将其从默认黑色更改的方法。有任何想法吗?

采纳答案by coneybeare

I have been able to make it work by subclassing a UITabBarController and using private classes:

我已经能够通过继承 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

回答by imnk

iOS 5 has added some new appearance methods for customising the look of most UI elements.

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

You can target every instance of a UITabBar in your app by using the appearance proxy.

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

For iOS 5 + 6:

对于 iOS 5 + 6:

[[UITabBar appearance] setTintColor:[UIColor redColor]];

For iOS 7 and above, please use the following:

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

[[UITabBar appearance] setBarTintColor:[UIColor redColor]];

Using the appearance proxy will change any tab bar instance throughout the app. For a specific instance, use one of the new properties on that class:

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

UIColor *tintColor; // iOS 5+6
UIColor *barTintColor; // iOS 7+
UIColor *selectedImageTintColor;
UIImage *backgroundImage;
UIImage *selectionIndicatorImage;

回答by pabugeater

I have an addendum to the final answer. While the essential scheme is correct, the trick of using a partially transparent color can be improved upon. I assume that it's only for letting the default gradient to show through. Oh, also, the height of the TabBar is 49 pixels rather than 48, at least in OS 3.

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

So, if you have a appropriate 1 x 49 image with a gradient, this is the version of viewDidLoad you should use:

所以,如果你有一个合适的 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];

}

回答by Marcin Zbijowski

When you just use addSubview your buttons will lose clickability, so instead of

当您只使用 addSubview 时,您的按钮将失去可点击性,而不是

[[self tabBar] addSubview:v];

use:

用:

[[self tabBar] insertSubview:v atIndex:0];

回答by Louis Gerbarg

There is no simple way to do this, you basically need to subclass UITabBar and implement custom drawing to do what you want. It is quite a bit of work for the effect, but it may be worth it. I recommend filing a bug with Apple to get it added to a future iPhone SDK.

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

回答by Gaurav

Following is the perfect solution for this. This works fine with me for iOS5 and iOS4.

以下是对此的完美解决方案。这对我来说适用于 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];
}

回答by capikaw

On iOS 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]];

I also recommend setting first depending on your visual desires:

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

[[UITabBar appearance] setBarStyle:UIBarStyleBlack];

The bar style puts a subtle separator between your view content and your tab bar.

栏样式在您的视图内容和标签栏之间放置了一个微妙的分隔符。

回答by Muhammad Rizwan

for me its very simple to change the color of Tabbar like :-

对我来说,更改 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"];

Try this!!!

尝试这个!!!

回答by jimmy

[[self tabBar] insertSubview:v atIndex:0];works perfectly for me.

[[self tabBar] insertSubview:v atIndex:0];非常适合我。

回答by Hsm

 [[UITabBar appearance] setTintColor:[UIColor redColor]];
 [[UITabBar appearance] setBarTintColor:[UIColor yellowColor]];