UITabBar change background color of one UITabBarItem on iOS7
You can add a subview to the parent tabBar, and set a background color on the subview. You can use the tabBar frame dimensions to calculate the offset and width of your tabBarItem, and then insert the subview underneath.
Example (in Swift):
// Add background color to middle tabBarItem
let itemIndex = 2
let bgColor = UIColor(red: 0.08, green: 0.726, blue: 0.702, alpha: 1.0)
let itemWidth = tabBar.frame.width / CGFloat(tabBar.items!.count)
let bgView = UIView(frame: CGRectMake(itemWidth * itemIndex, 0, itemWidth, tabBar.frame.height))
bgView.backgroundColor = bgColor
tabBar.insertSubview(bgView, atIndex: 0)
Changing Tint / Background color of UITabBar
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
Edit background color of specific Tab bar item
You can use the following code :
// Add background color to middle tabBarItem
let itemIndex = 2
let bgColor = UIColor(red: 0.08, green: 0.726, blue: 0.702, alpha: 1.0)
let itemWidth = tabBar.frame.width / CGFloat(tabBar.items!.count)
let bgView = UIView(frame: CGRectMake(itemWidth * itemIndex, 0, itemWidth, tabBar.frame.height))
bgView.backgroundColor = bgColor
tabBar.insertSubview(bgView, atIndex: 0)
For reference you can look into these Stackoverflow posts :
- Change background Color of One UITabBarItem
- Set background Color of Active Tab bar item
Change resizable background image/color of uiTabBarItem in iOS
Posting this in case someone might need it later.
This solution is made from three parts.
Part 1.
Set your UITabBar 'Item Positioning' property from Automatic to Centered. This will allow you to set the width of your tab bar items. The hight of the tab bar items will always be 49.
Part 2.
Use this function to create a UIImage from a color and a specific size:
- (UIImage *)imageFromColor:(UIColor *)color forSize:(CGSize)size withCornerRadius:(CGFloat)radius
{
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Begin a new image that will be the new image with the rounded corners
// (here with the size of an UIImageView)
UIGraphicsBeginImageContext(size);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip];
// Draw your image
[image drawInRect:rect];
// Get the image, here setting the UIImageView image
image = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
return image;
}
Part 3.
Now you are ready to actually set the tab items background color.
[[UITabBar appearance] setSelectionIndicatorImage:[self imageFromColor:[UIColor whiteColor] forSize:CGSizeMake(tabBar.itemWidth, 49) withCornerRadius:0]];
Why this isn't something implemented in the Interface Builder is beyond me. But this works.
Change tintColor of unselected UITabBarController item title and background image
Figured it out!
Use this to change the color of the text:
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor greenColor] }
forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }
forState:UIControlStateSelected];
And make sure that image rendering mode is set to ORIGINAL for the images
UIImage *deselectedImage = [[UIImage imageNamed:@"deselectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *selectedImage = [[UIImage imageNamed:@"selectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
faded, pale background color in UITabBar.background
I had a similar issue when programmatically creating the UITabBarController background color in iOS 8. The problem was I was using backgroundColor when I should have been be using barTintColor. Try this.
TabBarController* bc=(TabBarController*)self.window.rootViewController;
bc.tabBar.barTintColor=[UIColor greenColor];
Let me know if that works for you.
Related Topics
Initialization of 'Unsafepointer<Int>' Results in a Dangling Pointer
Disable iOS Reachability Swipe Gesture in iOS Game
Uidocumentinteractioncontroller Does Not Open Other App in iOS 11
Auto Layout How to Hide 1 View in a View with 3 Equal Width Views
How to Go to Specific Native View Controller from React-Native Code
How to Open Application Using Url
Uiwindow? Does Not Have Member Named Bounds
How to Lock Viewcontroller in Portrait Mode
How to Get Path to a Subfolder in Main Bundle
Nsdateformatter Still Parsing Instead Having Incorrect Format
Get Xcode 5 to Warn About New API Calls
How to Programmatically Delete My Own iOS App
How to Get the Index in Results of a Certain Realm Object
Change Cell Height by The Content of The Textview Inside The Cell
Is There an iOS API for Accessing Call Logs
Objc Protocol Implementation in Swift