MartinHN
MartinHN

Reputation: 19772

Set NavigationBar background to a solid color

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?

Upvotes: 10

Views: 13085

Answers (5)

Mingming
Mingming

Reputation: 2219

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]];

Upvotes: 0

Yunus Nedim Mehel
Yunus Nedim Mehel

Reputation: 12369

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]; 

Upvotes: 0

Lubiluk
Lubiluk

Reputation: 704

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

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];

Upvotes: 47

Jonathan Naguin
Jonathan Naguin

Reputation: 14766

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);

Upvotes: 5

kcho0
kcho0

Reputation: 169

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

Upvotes: -4

Related Questions