1

I'm currently doing the following:

    BlogViewController *viewController = [[BlogViewController alloc] initWithBlogPosts];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
    [self presentModalViewController:navController animated:YES];
    [viewController release];
    [navController release];

However, I need a way to use a custom UINavigationBar. I've tried using a category like this:

@implementation UINavigationBar (Image)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"toolbar.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

But it doesn't seem to pick that up. Any suggestions? BlogViewController is a subclass of UIViewController.

joshholat
  • 3,371
  • 9
  • 39
  • 48
  • 1
    See http://stackoverflow.com/questions/7657465/uinavigationbars-drawrect-is-not-called-in-ios-5-0 – Till Nov 19 '11 at 19:56
  • 1
    Or see my previous answer at http://stackoverflow.com/questions/704558/custom-uinavigationbar-background/6959354#6959354 – EricS Nov 19 '11 at 20:15
  • @EricS I am afraid your solution will not work once the App is linked against the iOS SDK Version 5 or higher. – Till Nov 19 '11 at 20:26
  • I got @Till's answer to work but only using the [UINavigationBar appearence method]. So iOS 4 support would be out. – joshholat Nov 19 '11 at 21:08
  • My way works fine in iOS 5 and when linked against the iOS 5 SDK -- I just double checked it. I use a subclass to override drawRect rather than a category. Using categories to override gives undefined behavior and breaks under iOS 5. – EricS Nov 20 '11 at 02:54
  • @EricS is your Nib just a blank one? – joshholat Nov 21 '11 at 15:00
  • You have to create CustomNavigationController.xib and put a UINavigationController in it and change the navigationBar class to "CustomNavigationBar". It's not pretty, but also not a hack in any way what will fail or anger Apple, so a decent solution. – EricS Nov 21 '11 at 21:50

3 Answers3

1

For ios5, you can set the background image directly using setBackgroundImage:forBarMetrics:.

Leave the category implementation around for pre-ios5, and check that the navigation bar responds to the selector above, and you've covered both versions.

jrturton
  • 118,105
  • 32
  • 252
  • 268
0

I used this guy's answer:

[self.navigationController setValue:[[[CustomNavBar alloc]init] autorelease] forKeyPath:@"navigationBar"];
Community
  • 1
  • 1
Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
0

I had a similar problem a while ago and then learned that you can actually do this on your app delegate:

After the #import... and before the implementation of the actual appDelegate.

Don't know if it helps but could be a good start point.

 @implementation UINavigationBar (Background)
 -(void)drawRect:(CGRect)rect {
 UIImage *image = [UIImage imageNamed:@"myNavBar.png"];
[image drawInRect:CGRectMake(0,0,self.frame.size.width, self.frame.size.height)];
}
@end

@implementation AppDelegate // code for appDelegate
Farini
  • 923
  • 1
  • 18
  • 35
  • Yup, I had tried that as well with no luck. Are you on iOS 5? – joshholat Nov 19 '11 at 20:32
  • 1
    Yes, you're both right. I just tried running on ios5 and it doesn't work. My bad. I'm trying to find a way around. If I do I'll post it right away. Thanks for reminding me to change the deployment target... – Farini Nov 19 '11 at 20:35