Changing Tint/Background Color of Uitabbar

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

How to definitively set UITabBar background color and tint color

If you want to set tabbar's tint and barTint color implicitly then in your Appdelegate.swift,

    UITabBar.appearance().barTintColor = .orange
UITabBar.appearance().tintColor = .green

If you want to set tabbar's tint and barTint color for specific viewController then in ViewController.swift,

 self.tabBarController?.tabBar.tintColor = .orange
self.tabBarController?.tabBar.barTintColor = .green

Changing the background color of Tab Bar

To change background colour of UITabBar

TabBarController* Tcontroller =(TabBarController*)self.window.rootViewController;
Tcontroller.tabBar.barTintColor=[UIColor yourcolour];

Swift 3

Based on the code above, you can get it by doing this

let Tcontroller = self.window.rootViewController as? UITabBarController
Tcontroller?.tabBar.barTintColor = UIColor.black // your color

or in more general

UITabBar.appearance().barTintColor = UIColor.black // your color

Change Tabbar background color in override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!)

Got Solution , making Completely transparent UITabBar in app delegate

[[UITabBar appearance] setBarTintColor:[UIColor clearColor]];
[[UITabBar appearance] setBackgroundImage:[UIImage new]];
[[UITabBar appearance] setShadowImage:[UIImage new]];
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];

And changing background color on didSelectItem

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
println(" selected index \(item.tag)")

if(item.tag == 0){
dismissViewControllerAnimated(true, completion: nil)
}

if(item.tag == 1){
self.view.backgroundColor = Colors.TAB_BAR_ALBUM_BG_COLOR

}else if(item.tag == 2){
self.view.backgroundColor = Colors.TAB_BAR_ME_BG_COLOR

}
}

How to change the background color of tab bar programatically?

You should use self.tabBar.barTintColor or have a look at UIBarStyle and self.tabBar.barStyle and see if that works.

UITabBar not changing tint color Xcode 9.3

Basically, when you want to change the tint color of UITabBar programmatically, UITabBar class gives you several tint color properties:

  • tintColor: TabBarItem's color.
  • barTintColor : TabBar's background bar's color.
  • unselectedItemTintColor : color of unselected items.

so if you change the tintColor, barItems' color would be changed.



...but, Why it doesn't works on IB?

When you set a specific color to UITabBar's item in IB, there's an option named Image Tint.

imageTint

Changing a Tint option on "View" section won't affect anything to TabBar's items but only Image Tint option can change tabBar's item color.

storyboard's global tint color option changes Tint option of "View" section, but doesn't affect default value of Image Tint option, so It doesn't affect the tab bar's tint color.



So.. Why Image Tint option doesn't affected?

I can't explain why doesn't it affected. Maybe Apple had an issue with this, or kind of bug.


there are some workarounds for setting an image color :

  • Explicitly Set an Image Tint option to UITabBarController's TabBar object.

You may should set every TabBarController's Image Tint option, because it doesn't affects global setting.

  • Programmatically change global UITabBar's tintColor.

At AppDelegate.swift's didFinishLaunchingWithOptions, paste following code

UITabBar.appearance().tintColor = <#Color what you want#>

How to change tint color of tab bar in swift?

To solve the problems in your AppDelegate do this:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

UITabBar.appearance().tintColor = UIColor.whiteColor()
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState: UIControlState.Normal)

return true
}

And in your ViewController do something like this:

extension UIImage {
func makeImageWithColorAndSize(color: UIColor, size: CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(CGRectMake(0, 0, size.width, size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}

extension UIImage {
func imageWithColor(tintColor: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)

let context = UIGraphicsGetCurrentContext()! as CGContextRef
CGContextTranslateCTM(context, 0, self.size.height)
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, CGBlendMode.Normal)

let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
CGContextClipToMask(context, rect, self.CGImage)
tintColor.setFill()
CGContextFillRect(context, rect)

let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
UIGraphicsEndImageContext()

return newImage
}
}

class FirstViewController: UIViewController {

var tabBar: UITabBar?

override func viewDidLoad() {
super.viewDidLoad()

tabBar = self.tabBarController!.tabBar
tabBar!.selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar!.frame.width/CGFloat(tabBar!.items!.count), tabBar!.frame.height))

// To change tintColor for unselected tabs
for item in tabBar!.items! as [UITabBarItem] {
if let image = item.image {
item.image = image.imageWithColor(UIColor.whiteColor()).imageWithRenderingMode(.AlwaysOriginal)
}
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

*The first extension to UIImage is taken from another question by the same author: How to change default grey color of tab bar items?

Change only one specific UITabBarItem tint color

I did some experimenting and based on this answer, found a way to do what I want without subclassing UITabBarItem or UITabBar!

Basically, the idea is to create a method of UIImage that mimics the tint mask behavior of UITabBar, while rendering it in its "original" form and avoiding the native tint mask.

All you have to do is create a new instance method of UIImage that returns an image masked with the color we want:

@interface UIImage(Overlay)
- (instancetype)tabBarImageWithCustomTint:(UIColor *)tintColor;
@end

@implementation UIImage(Overlay)

- (instancetype)tabBarImageWithCustomTint:(UIColor *)tintColor
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextClipToMask(context, rect, self.CGImage);
[tintColor setFill];
CGContextFillRect(context, rect);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
newImage = [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
return newImage;
}
@end

This is a fairly straightforward version of the code in the answer that I posted, with one exception- the returned image has its rendering mode set to always original, which makes sure that the default UITabBar mask won't be applied. Now, all that is needed is to use this method when editing the tab bar item:

navController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"title" image:normal_image selectedImage:[selected_image tabBarImageWithCustomTint:[UIColor redColor]]];

Needless to say, selected_image is the normal image one gets from UIImage imageNamed: and the [UIColor redColor can be replaced with any color one desires.



Related Topics



Leave a reply



Submit