I want to exploit the new iOS 5 appearance API to supply custom background images to all UINavigationBar instances in my app. To do this, it\'s as simple as this:
You can either set it globally with the class appearance proxy or set it on an instance of a navBar.
I'm currently setting background on an instance of the nav bar and it seems to be working. I have two different navBars with different backgrounds. If you set it on an instance, you should be able to condition the code.
UINavigationController *myNavController = [[UINavigationController alloc] initWithRootViewController:myView];
[viewControllers addObject:myNavController];
// not supported on iOS4
UINavigationBar *navBar = [myNavController navigationBar];
if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
// right here, you could condition bg image based on properties of this instance
// of the navBar or any other condition.
[navBar setBackgroundImage:[UIImage imageNamed:@"bg.jpg"] forBarMetrics:UIBarMetricsDefault];
}
If you want to set using the class method, you can set for all:
[[UINavigationBar appearance] setBackground ...
I will admit though that it's pretty new and I'm just figuring it out like most folks.