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?
|
||
|
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
|
||||||||||
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
|
||||||||
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];
|
||||
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.
|
||||
feedback |
up vote 2 down vote |
[v setBackgroundColor ColorwithRed: Green: Blue: ];
|
||
feedback |
up vote 2 down vote |
[[self tabBar] insertSubview:v atIndex:0]; works perfectly for me.
|
|||
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.
|
||
feedback |
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2011/12/13/2285784.html