10

Is there any way I can set the background of the Navigation Bar of the UINavigationController to a solid color?

I know I can change the Tint color, but that still leaves me with the gradient/glass effect.

Any way I can get rid of that, and just have a plain old solid color?

Undo
  • 25,519
  • 37
  • 106
  • 129
MartinHN
  • 19,542
  • 19
  • 89
  • 131

5 Answers5

47

The following code also results in solid color of navigation bar:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];
darksky
  • 20,411
  • 61
  • 165
  • 254
Lubiluk
  • 704
  • 5
  • 12
  • 2
    Be sure to also set the tint color or your buttons on the nav bar will not get the color: [[UINavigationBar appearance] setTintColor:[UIColor redColor]]; –  Jul 16 '13 at 04:56
  • Where do you do this? I'm trying to do it in the viewDidLoad of the view controller with the navigation bar and it does not seem to take effect. Does it need to be done sometime earlier. I have a storyboard which appears to be overriding it. – David Mar 24 '15 at 14:03
  • David, it has to be done before you present the view. Configuring "appearance" doesn't update visible views. If you want to set this globally the best place to do it would be in application:didFinishLaunchingWithOptions:. – Lubiluk Mar 25 '15 at 14:38
5

I think you have to subclass UINavigationBar and override -(void)drawRect:(CGRect)rect:

UIColor *colorFlat = /* any UIColor*/
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [colorFlat CGColor]);
CGContextFillRect(context, rect);
Jonathan Naguin
  • 14,526
  • 6
  • 46
  • 75
0

I used to override drawRect method and fill color; but after iOS7 upgrade it causes some problems on UINavigationBar. If you write your own drawrect method, even if you call [super drawRect], it changes the bar's dimension and you end up with a navigationBar with 44 pixels height. The status bar is left empty.

To get a solid colored navigationBar, I used an image as background image (any small solid colored image will do since you are stretching it) and added this lines inside the initWithFrame method of the UINavigationBar subclass:

[self setBackgroundColor:[UIColor clearColor]]     
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"bgimage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault]; 
Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56
0

The following code worked for me:

[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];
self.navigationController.navigationBar.translucent = NO;
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
Mingming
  • 2,189
  • 20
  • 21
-4

Create a CustomUIViewController that extends UIViewController and override viewDidLoad to something like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0];
}

After that, use your CustomUIViewController in your controllers.

Credits

Community
  • 1
  • 1
kcho0
  • 169
  • 1
  • 1
  • 7