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.

Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
BkdD
  • 593
  • 1
  • 7
  • 13

7 Answers7

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jageen
  • 6,345
  • 2
  • 37
  • 56
  • Ah yes, I just tried and works! Can't believe i haven't seen this when i was searching earlier. Thanks a lot. – BkdD Oct 16 '13 at 14:34
  • 1
    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](https://docs.google.com/file/d/0B4pmO_xiSW_4SjQ1N0xBeGpFUG8/edit?usp=sharing) 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? – BkdD Oct 16 '13 at 15:15
  • 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 ? – Jageen Oct 17 '13 at 05:23
  • Thank you! I spent way too much time on this one trying to figure out what would let me change the background color. –  Jan 22 '15 at 00:51
  • @BkdD did you manage to change using colorWithRed:green:blue:alpha? it only works for certain values for me. Very weird behavior – Lucas Jan 22 '16 at 13:24
  • If only I'd read this two hours ago. Someone give that guy a taco – bodacious Apr 12 '16 at 19:11
  • Why does .backgroundColor even exist for toolbars then? –  Jul 17 '18 at 19:25
  • I did not remember but i think it is for supporting older version i.e before ios 7 – Jageen Jul 18 '18 at 03:20
  • you must also set translucent to false – Ilias Karim Mar 02 '20 at 16:14
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...

Ashish
  • 1,899
  • 20
  • 22
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;
Ilias Karim
  • 4,798
  • 3
  • 38
  • 60
Spencer Evison
  • 204
  • 3
  • 5
1

Try this on IOS 10:

let dummyToolbar = UIToolbar()
dummyToolbar.barTintColor = .lightGray
dummyToolbar.sizeToFit() // without this line it doesn't work
Adrian
  • 19,440
  • 34
  • 112
  • 219
1

Swift 4+:

toolBar.barTintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
toolBar.isTranslucent = false
toolBar.sizeToFit()
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
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;
Mutablegopi
  • 281
  • 2
  • 11
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)

    }
Yogesh Lolusare
  • 2,162
  • 1
  • 24
  • 35