I am a very rookie with iOS development, but after a good amount of searching for my answer, I have not yet been able to find it. I am now considering using pure image views over a tab bar, even though Id love to use the tab bar element. My problem is I don't see any way to change the background colour of the tab bar button cell when I select that button. I can change the tint(the colour of the image i used for the button). Is there a war to simply change the background when selected (I am using a white-ish image for when button is selected, hence my need to change background colour of the cell), or should I just use imageView to get my desired need?
2 Answers
EDIT 2 : I think you can achieve your requirements using selectedImageTintColor: (deprecated) or selectionIndicatorImage: methods from UITabBar.
Old replies :
According to the documentation: https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UITabBarItem_Class/index.html#//apple_ref/occ/instp/UITabBarItem/selectedImage
There is a convenient property selectedImage to do the stuff.
Use can set it directly in the init method. Example in UIViewController :
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = @"my controller";
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:self.title image:[UIImage imageNamed:@"normalTabBarItem"] selectedImage:[UIImage imageNamed:@"selectedTabBarItem"]];
}
return self;
}
EDIT : A UITabBarItem doesn't have a background color. So you must embed the "background color" directly in the image and selected image.
By default, the actual unselected and selected images are automatically created from the alpha values in the source images. To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal.

- 252
- 3
- 12
-
My problem is I want to change the background colour of the whole space the tab bar button fills out, not just the selectedImage.... which I pointed out in my question. my selectedImage is fine. – awp2004 Nov 20 '14 at 15:32
-
I have edited my answer. If it still doesn't satisfy yourself, can you post images of what you want to achieve ? – roborg Nov 20 '14 at 16:19
-
In the meantime since my last reply I was having thoughts in that direction. But I was thinking that when I then rotate the screen (and widening the tab bar still full width) the image wouldn't stretch? I will try it tar when I have time. I blame the transparent pics the company has send me while wanting what I described lol. I will get back in an hour.(does approving your answer close my thread? I still want a little more enlightening) – awp2004 Nov 20 '14 at 16:49
-
I have updated my comment. It seems that UITabBar have convenient methods to do this. Tell me if it's sufficient. Here are others topics related to your problem : http://stackoverflow.com/questions/17879066/how-do-i-change-background-color-of-uitabitem-when-item-is-selected http://stackoverflow.com/questions/571028/changing-tint-background-color-of-uitabbar – roborg Nov 20 '14 at 17:23
-
[[UITabBar appearance] setSelectionIndicatorImage:blackbk]; does this call just affect all tab bars that I would make in my app? Normally I would think I would somehow have to refer (or outlet) the tab bar that I'm customising – awp2004 Nov 20 '14 at 17:59
-
Is there any way I can get it to autosize to fill out the tabbaritem cell ( I tried to sue the above call)? I suspect fail appearance when somebody rotate (or use a different device), which normally wouldn't be a problem with an UIImage. Or am I meant to create images for all different kind of sizes of screens that I intend the app will run on? I still see the autosizing as being more bulletproof in case of an unexpected screen size. – awp2004 Nov 20 '14 at 18:04
-
Thanks for your help though, I will give you a green mark (does it add points to ones profile when getting a correct answer?) – awp2004 Nov 20 '14 at 18:10
-
[[UITabBar appearance] setSelectionIndicatorImage:blackbk]; does this call just affect all tab bars : yes. See http://nshipster.com/uiappearance/ If you don't want this to all your tabbars, just do [mytabbar selectionIndicatorImage:myImage] – roborg Nov 20 '14 at 18:11
-
If setSelectionIndicatorImage: give you pain, just use selectedImageTintColor:. You will fix this deprecation when you'll be more at ease in resizing image. – roborg Nov 20 '14 at 18:17
-
does it add points to ones profile when getting a correct answer? : yes, see http://stackoverflow.com/help/whats-reputation – roborg Nov 20 '14 at 18:18
-
I thank you for your time (rep add:))! I was just trying to understand why that call would affect all tab bars i make. is it the appearance msg that does that, or is it something I can use to modify other elements in the same easy way. – awp2004 Nov 20 '14 at 18:23
-
The appearance message does that. You can use it on other classes, see also Apple documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAppearance_Protocol/index.html#//apple_ref/occ/intfcm/UIAppearance/appearance. – roborg Nov 20 '14 at 22:28
To change selected tab bar item background I made custom class called TabBar with following init:
#import "TabBar.h"
#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
@implementation TabBar
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (!(self = [super initWithCoder:aDecoder])) return nil;
[self setupSelectionIndicatorImage];
return self;
}
- (void)setupSelectionIndicatorImage {
UIView *item = self.subviews.firstObject;
UIColor *backgroundColor = RGB(0, 117, 255);
// Creating background image
CGRect rect = CGRectMake(0, 0, item.bounds.size.width, item.bounds.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0); // 0 == device main screen scale
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [backgroundColor CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.selectionIndicatorImage = img;
}
@end

- 1,296
- 17
- 17