1

Can I do this without overriding the drawRect method?

I've tried the following:

UIImage *navBarImage = [[UIImage imageNamed:@"image_navbg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
[self.descriptionBar setBackgroundColor:[UIColor colorWithPatternImage:navBarImage]];
[self.bottomBar setBackgroundColor:[UIColor colorWithPatternImage:navBarImage]];

Where image_navbg.png is a solid matte black square. The result I get still has a black - gray gradient.

Also setting these did not help:

[self.descriptionBar setTranslucent:YES];
[self.bottomBar setTranslucent:YES];
Andrew Lauer Barinov
  • 5,694
  • 10
  • 59
  • 83
  • Check this answer - http://stackoverflow.com/questions/4904877/remove-gradient-on-uinavigationbar – kgu87 Jul 24 '12 at 01:49

3 Answers3

3

I wanted to do the same thing with the toolbar that's built-in to the navigation controller. You reveal it using:

[self.navigationController setToolbarHidden:NO];

In order to remove the gradient on this kind of toolbar (in iOS 4 and earlier at least), I used the following technique with success. First I added this to my appDelegate header:

@interface MyToolbar : UIToolbar {}

- (void)drawRect:(CGRect)rect;

@end

Then I added this to my appDelegate .m file

@implementation MyToolbar

- (void)drawRect:(CGRect)rect {
    UIColor *colorTabBlack = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColor(context, CGColorGetComponents( [colorTabBlack CGColor]));
    CGContextFillRect(context, rect);
}
@end

Then in IB I switched the (now visible) toolbar's class to "MyToolbar" - it took me forever to figure this out, which is why I'm posting it. Hope it helps someone else!

Tim
  • 162
  • 5
2

If you’re targeting iOS 5, use UIAppearance for this — don’t override -drawRect: anymore.

Evadne Wu
  • 3,190
  • 1
  • 25
  • 25
0

This is the way I did it. Worked great for me. You might have to try different indexes in your subviews.

// Change color of searchbar
[[self.searchbar.subviews objectAtIndex:0] removeFromSuperview];
self.searchbar.backgroundColor = [UIColor blackColor];

// Change color of toolbar
[[self.toolbar.subviews objectAtIndex:0] removeFromSuperview];
self.toolbar.backgroundColor = [UIColor blackColor];
Sean Greevers
  • 235
  • 1
  • 5
  • 14