TommyG
TommyG

Reputation: 4155

overriding UINavigationBar drawRect function doesnt work

I have a simple app with a nav bar controller and I would like to change its navigation bar while overriding the drawRect function. I read in many places here that all I need to do is just to paste the code below above my appDelegate. Sadly, it doesnt seem to do anything for me. I tried as a start to just have the color of the nav bar changed, before i try to add an image to it, but again, nothing happens. What am i missing here

I tired to insert this code above the AppDelegate:

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {
 UIColor *color = [UIColor redColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
CGContextFillRect(context, rect);
  }
 @end

And with the background image:

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {
// Drawing code 
UIImage *img = [UIImage imageNamed: @"13.png"];
[img drawInRect:CGRectMake(0, 0, 320, self.frame.size.height)];    
}
@end

Thanks for the help!

Upvotes: 0

Views: 1336

Answers (2)

user756245
user756245

Reputation:

This code declares a category on UINavigationBar and redefines the behaviour of its drawRect: method. Try to put the code snippet (second one) at the end of the .m file of the app delegate.

Although the first should work, another more clean option is to write the category in a separate file you will include in the app delegate.

Also make sure the picture you are loading exists in the app bundle.

Upvotes: 2

TommyG
TommyG

Reputation: 4155

Ok - solution found! This doesnt seem to work on the iPhone 5.0 simulator!

Upvotes: 0

Related Questions