UIToolbar setBackgroundColor doesn't fully change color
Asked Answered
I

7

39

I'm trying to set the background color of a UIToolBar. I tried selecting the color from IB's Attribute Inspector, and tried setting it programmatically through setBackgroundColor:[UIColor ...].

Both solutions work, but only partially: the color blends something like 50% with white and the toolbar is very light...doesn't show the color I actually chose, but a much lighter version of it.

How can I have the UIToolBar of the actual color I'm choosing? It's probably very simple to solve, but I can't find a way and can't find answers online either.

Intine answered 16/10, 2013 at 10:51 Comment(0)
S
106

Write below code in your viewDidLoad

self.navigationController.toolbar.barTintColor = [UIColor redColor];

It will set red color as your tool bar background.

Reference link https://web.archive.org/web/20160321155823/https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/Bars.html#//apple_ref/doc/uid/TP40013174-CH8-SW5

In it they said that Use barTintColor to tint the bar background. enter image description here

Shuttlecock answered 16/10, 2013 at 11:53 Comment(9)
Ah yes, I just tried and works! Can't believe i haven't seen this when i was searching earlier. Thanks a lot.Intine
sorry, further question... I'm trying now with a more specific color, building it with UIColor colorWithRed:green:blue:alpha: but the color again doesn't show up properly.. this time basically keeps the same hue but drops saturation and brightness of about 25%, here is a picture with it: !image in the picture you see the left color is the one i want, the right one how it becomes actually. do you have any idea of what i'm doing wrong?Intine
Sorry, for late reply i was busy, i dont have any idea but, i will check it when get free. and if you got solution let me know k ?Shuttlecock
Thank you! I spent way too much time on this one trying to figure out what would let me change the background color.Dimension
@Intine did you manage to change using colorWithRed:green:blue:alpha? it only works for certain values for me. Very weird behaviorSalley
If only I'd read this two hours ago. Someone give that guy a tacoEmbellish
Why does .backgroundColor even exist for toolbars then?Flocculus
I did not remember but i think it is for supporting older version i.e before ios 7Shuttlecock
you must also set translucent to falseBricklayer
S
27

IN iOS 7 you need to set the barTintColor Property-

UIToolbar *doneToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 584, 320, 44)];
doneToolbar.translucent=NO;
doneToolbar.barTintColor=[UIColor redColor];
[self.view addSubview:doneToolbar];

I have used it its working fine...

Sruti answered 13/1, 2014 at 5:21 Comment(0)
T
6

In addition to Jageen's answer, you must also set the translucent property to false. Otherwise the color will have slightly less saturation and hue than what is specified with barTintColor.

// Sets to a specific color
self.navigationController.toolbar.barTintColor = [UIColor colorWithRed:6.0 / 255.0 green:52.0 / 255.0 blue:90.0 / 255.0 alpha:1.0];

// Without this, color will be faded slightly and not exactly what's specified above  
self.navigationController.toolbar.translucent = false;
Trimer answered 24/5, 2017 at 2:1 Comment(0)
B
1

Try this on IOS 10:

let dummyToolbar = UIToolbar()
dummyToolbar.barTintColor = .lightGray
dummyToolbar.sizeToFit() // without this line it doesn't work
Bicameral answered 28/4, 2017 at 8:14 Comment(0)
R
1

Swift 4+:

toolBar.barTintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
toolBar.isTranslucent = false
toolBar.sizeToFit()
Rhizo answered 17/9, 2019 at 8:57 Comment(0)
N
0

UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];

numberToolbar.backgroundcolor = [UIColor redcolor]; numberToolbar.items = [NSArray arrayWithObjects: [[UIBarButtonItem alloc]initWithTitle:@"Clear" style:UIBarButtonItemStyleBordered
nil];

[numberToolbar sizeToFit];
numberTextField.inputAccessoryView = numberToolbar;
Nauseating answered 12/12, 2014 at 5:31 Comment(0)
R
0

Throughout App:

    UIToolbar.appearance().barTintColor = TOOLBAR_BACKGROUND_COLOR

if let font = UIFont(name: "AvenirNext-DemiBold", size: 15) {
        UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName:TOOLBAR_TITLE_COLOR], forState: UIControlState.Normal)

    }
Rapper answered 18/11, 2015 at 7:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.