0

I have a UINavigationController that I'm creating programatically:

 MessageTableViewController* homeStream = [[MessageTableViewController alloc] initWithNibName:@"MessageTableViewController" bundle:nil];
          homeStream.fetchHomeStream = YES;
          homeStream.title = @"Home";
          UINavigationController* navBarController = [[UINavigationController alloc] initWithRootViewController:homeStream];
          navBarController.tabBarItem.image = [UIImage imageNamed:@"hometab.png"];
          [homeStream release];

I have a custom UINavigationBar called STNavigationBar. How can I use STNavigationBar instead of the default UINavigationBar that is part of the UINavigationController?

Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556

2 Answers2

3

I ended up using the iOS5 UIKit appearance proxy:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"header"] forBarMetrics:UIBarMetricsDefault];

No subclassing, or categories needed.

Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
0

Edit for future readers, the answer below does not work anymore on iOS 5.

You can't just set it to the existing instance, because UINavigationController has a readonly property for navigationBar. And I don't think subclassing a UINavigationController is what you want.

I recon the best way to do this is to create a category that extends a UINavigationBar, as discussed here.

Wolfert
  • 974
  • 6
  • 12
  • I originally had it as a category which worked fine, but iOS5 breaks this functionality, and it is now bad practice to use categories to over-ride actual methods. So I decided to subclass it. It works when I set the navigation bar in IB, but I also need to set it programatically... – Sheehan Alam Oct 23 '11 at 22:45
  • I was not aware of that. What about option no. 3 in this answer? http://stackoverflow.com/questions/1869331/set-programmatically-a-custom-subclass-of-uinavigationbar-in-uinavigationcontrol/3080665#3080665 – Wolfert Oct 23 '11 at 22:50
  • Just posted an answer, I used iOS5 UIKit appearance proxy – Sheehan Alam Oct 24 '11 at 02:28