Changing Tint / Background color of UITabBar

简介:

28

down vote

favorite

46

share [fb]

share [tw]

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?

iphone uitabbarcontroller

link|edit

asked Feb 20 '09 at 20:02


pixel

2,15451824


86% accept rate



These are great answers. If you're allowing autorotation, it's helpful to set the autoresizingMask of the subview to have flexible margins and size, or the new background won't resize with the tab bar. – pmjordan Jul 2 '10 at 16:52

up vote

31

down vote

accepted

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

@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

link|edit

answered Mar 14 '09 at 17:17


coneybeare

12.6k33162



This is sort of an odd solution in that it just places semi transparent brown rectangle on top of the tabbar. The problem is that all the buttons are turned brown, not just the background. However this seems to be the best option anyone's presented so far. – Jonah Aug 18 '09 at 20:04


is this a private API? if i used this in my app would i get refused? – Frank Nov 16 '10 at 18:23


there doesnt appear to be any private calls in there no – tigermain Nov 25 '10 at 18:02


the tabbar property used to not be accessible. – coneybeare Nov 26 '10 at 18:24


this modifies the tabbar background, not the "selected item" highlight, which i would be more interested in – Ryan Sullivan Nov 27 '10 at 20:33

show 3 more comments

feedback

up vote

25

down vote

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.

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

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


}

Here's what the UITabBar looks like: http://www.lehigh.edu/sol0/TabBarGradient.jpg

link|edit

answered Dec 30 '09 at 20:51


pabugeater

25132


1

 

if you want to make it pretty with a background picture, see this: duivesteyn.net/2010/01/16/iphone-custom-tabbar-background-image – norskben Feb 12 '10 at 12:58


I think it needs to be: [[self tabBar] insertSubview:v atIndex:0]; – Vibhor Goyal Aug 17 '10 at 20:49


I know the answer is old but that worked for me. Since i needed a custom image instead of a color, i mixed your answer with this one tint on bars - UITabBar part – RaphaelDDL Nov 18 at 19:25

feedback

up vote

17

down vote

when you just use addSubview your buttons will loose clickability, so instead of

[[self tabBar] addSubview:v];

use:

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

link|edit

answered Oct 5 '09 at 10:45


Marcin Zbijowski

19917


1

 

thanks it worked for me.. – Biranchi Apr 2 '10 at 6:03

feedback

up vote

4

down vote

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.

link|edit

answered Feb 20 '09 at 20:11


Louis Gerbarg

20.3k3656


1

 

Sir, did you filled up a bug with apple? – Spark Oct 30 '09 at 18:37

feedback

up vote

2

down vote

[v setBackgroundColor ColorwithRed: Green: Blue: ];

link|edit

answered Jun 8 '09 at 6:45


jlthero


feedback

up vote

2

down vote

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

link|edit

edited Aug 4 at 19:15


Legolas

3,3411218

answered Jun 3 '10 at 18:12


jimmy

632


feedback

up vote

0

down vote

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. Then set a an ImageView control on the bottom of the MainWindow nib with your custom tabbar image underneath the alpha'ed tabBarCOntroller. Then swap the images, change colors or hightlight when the tabbarcontroller switches views.

However, you lose the '...more' and customize functionality.

link|edit

answered Aug 3 at 11:09


user855723

93


feedback

 

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!












本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/archive/2011/12/13/2285784.html ,如需转载请自行联系原作者


相关文章
|
4月前
|
前端开发 JavaScript
【HTML+CSS+JavaScript】3d-boxes-background
【HTML+CSS+JavaScript】3d-boxes-background
32 0
|
6月前
|
搜索推荐 UED
什么是计算机图形领域的 color saturation
什么是计算机图形领域的 color saturation
|
6月前
|
前端开发
背景色[background-color]
背景色[background-color]。
37 6
|
6月前
|
前端开发
前景色[color]
前景色[color]。
34 0
COLOR
COLOR
112 0
关于 QWidget无法使用stylesheet设置自身backgound-color(背景色)和border-image(背景图片) 的解决方法
关于 QWidget无法使用stylesheet设置自身backgound-color(背景色)和border-image(背景图片) 的解决方法
关于 QWidget无法使用stylesheet设置自身backgound-color(背景色)和border-image(背景图片) 的解决方法
|
前端开发 JavaScript API
How to change the background color of button in Footer area
How to change the background color of button in Footer area
125 0
How to change the background color of button in Footer area
|
算法 Windows
1054. The Dominant Color (20)
Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of information for each pixel.
1060 0