简体   繁体   English

改变UITabBar的色调/背景颜色

[英]Changing Tint / Background color of UITabBar

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

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;

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

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. 哦,同样,TabBar的高度是49像素而不是48像素,至少在OS 3中是这样。

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

}

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

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. 条形样式在视图内容和标签栏之间放置一个微妙的分隔符。

There is no simple way to do this, you basically need to subclass UITabBar and implement custom drawing to do what you want. 没有简单的方法可以做到这一点,你基本上需要子类UITabBar并实现自定义绘图来做你想要的。 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. 我建议向Apple提交一个错误,将其添加到未来的iPhone SDK中。

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];
}

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!!! 试试这个!!!

[[self tabBar] insertSubview:v atIndex:0]; works perfectly for me. 适合我。

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

for just background color 只是背景颜色

Tabbarcontroller.tabBar.barTintColor=[UIColor redcolour];

or this in App Delegate 或者在App Delegate中

[[UITabBar appearance] setBackgroundColor:[UIColor blackColor]];

for changing color of unselect icons of tabbar 用于更改tabbar的取消选择图标的颜色

For iOS 10: 对于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];
}

Above iOS 10: iOS 10以上:

// this need to be in appdelegate didFinishLaunchingWithOptions
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blackColor]];

There are some good ideas in the existing answers, many work slightly differently and what you choose will also depend on which devices you target and what kind of look you're aiming to achieve. 现有答案中有一些好主意,许多工作略有不同,您选择的还取决于您定位的设备以及您希望实现的外观。 UITabBar is notoriously unintuitive when it come to customizing its appearance, but here are a few more tricks that may help: UITabBar在定制它的外观方面是出了名的不直观,但是这里有一些可能有用的技巧:

1). 1)。 If you're looking to get rid of the glossy overlay for a more flat look do: 如果你想摆脱光面覆盖层以获得更平坦的外观,请执行以下操作:

tabBar.backgroundColor = [UIColor darkGrayColor]; // this will be your background
[tabBar.subviews[0] removeFromSuperview]; // this gets rid of gloss

2). 2)。 To set custom images to the tabBar buttons do something like: 要将自定义图像设置为tabBar按钮,请执行以下操作:

for (UITabBarItem *item in tabBar.items){
    [item setFinishedSelectedImage:selected withFinishedUnselectedImage:unselected];
    [item setImageInsets:UIEdgeInsetsMake(6, 0, -6, 0)];
}

Where selected and unselected are UIImage objects of your choice. selectedunselected是您选择的UIImage对象。 If you'd like them to be a flat colour, the simplest solution I found is to create a UIView with the desired backgroundColor and then just render it into a UIImage with the help of QuartzCore. 如果您希望它们是平面颜色,我发现最简单的解决方案是使用所需的backgroundColor创建一个UIView ,然后在QuartzCore的帮助下将其渲染为UIImage I use the following method in a category on UIView to get a UIImage with the view's contents: 我在UIView的类别中使用以下方法来获取带有视图内容的UIImage

- (UIImage *)getImage {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [[UIScreen mainScreen]scale]);
    [[self layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

3) Finally, you may want to customize the styling of the buttons' titles. 3)最后,您可能想要自定义按钮标题的样式。 Do: 做:

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];
}

This lets you do some adjustments, but still quite limited. 这可以让你做一些调整,但仍然非常有限。 Particularly, you cannot freely modify where the text is placed within the button, and cannot have different colours for selected/unselected buttons. 特别是,您无法自由修改文本在按钮中的放置位置,并且不能为选定/未选择的按钮设置不同的颜色。 If you want to do more specific text layout, just set UITextAttributeTextColor to be clear and add your text into the selected and unselected images from part (2). 如果你想要做更具体的文本布局,只需设置UITextAttributeTextColor要清楚,并添加文本到selectedunselected从部分(2)的图像。

[v setBackgroundColor ColorwithRed: Green: Blue: ];
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.0 answer: (from Vaibhav Gaikwad) Swift 3.0回答:(来自Vaibhav Gaikwad)

For changing color of unselect icons of tabbar: 要更改tabbar的取消选择图标的颜色:

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)
        }
}

For changing text color only: 仅用于更改文本颜色:

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .normal)

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red, for: .selected)

使用AppDelegate外观的Swift 3执行以下操作:

UITabBar.appearance().barTintColor = your_color

Another solution (which is a hack) is to set the alpha on the tabBarController to 0.01 so that it is virtually invisible yet still clickable. 另一个解决方案(这是一个hack)是将tabBarController上的alpha设置为0.01,这样它几乎不可见但仍然可以点击。 Then set a an ImageView control on the bottom of the MainWindow nib with your custom tabbar image underneath the alpha'ed tabBarCOntroller. 然后使用alpha'ed tabBarCOntroller下面的自定义tabbar图像在MainWindow笔尖的底部设置一个ImageView控件。 Then swap the images, change colors or hightlight when the tabbarcontroller switches views. 然后在tabbarcontroller切换视图时交换图像,更改颜色或高亮度。

However, you lose the '...more' and customize functionality. 但是,您失去了“...更多”和自定义功能。

Hi There am using iOS SDK 4 and i was able to solve this issue with just two lines of code and it's goes like this 嗨我在使用iOS SDK 4,我只用两行代码解决了这个问题,就像这样

tBar.backgroundColor = [UIColor clearColor];
tBar.backgroundImage = [UIImage imageNamed:@"your-png-image.png"];

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM