1

I want to replace the current nav bar with a custom image. How my code is structured is that a tab bar controller controls a bunch of navigation controllers which contain views (tab bar controller -> nav controller -> view). I tried using this code in my app delegate

@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
  UIColor *color = [UIColor blackColor];
  UIImage *img  = [UIImage imageNamed: @"nav.png"];
  [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
  self.tintColor = color;
}
@end

But it did not work. Any ideas why? Should I have placed it somewhere else? Thanks!

bkcw5
  • 21
  • 3

2 Answers2

3

if you are using iOS 5, you can use setBackgroundImage: like this where ever you initialized the navigation controller (aNavigationController in this example):

[aNavigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav.png"] forBarMetrics:UIBarMetricsDefault];
JAB
  • 3,165
  • 16
  • 29
2

I would not use a category. I would subclass UINavigationBar instead. See this: https://stackoverflow.com/a/6959354/472344

If you are targeting iOS 5 or up, use @BJH's solution instead.

Community
  • 1
  • 1
Darren
  • 10,091
  • 18
  • 65
  • 108