Reputation: 2221
I used these suggested solutions to give my UINavigationBar a custom image. (To make it more explicit: I added a category to UINavigationBar in my AppDelegate.m file). This worked fine so far and I didn't have any problems. Now however, I was running my app on the recent iOS5 beta. The UINavigationBar is blank now again.
Since all the other apps I have installed, that use a custom image, still behave the same there must be something "wrong" in my code I guess, that iOS5 now doesn't support anymore.
So does anybody have an idea what could be the problem with my adoption of the above mentioned solutions?
The only way I found to make it work was to create a real subclass of UINavigationBar and then in all the views tell IB to use that custom class. Not as elegant, though...
Upvotes: 4
Views: 16620
Reputation: 2730
Use this code
UIImage *backgroundImage = [UIImage imageNamed:@"strip.png"];
[upnavbar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
this wil work.
Upvotes: 1
Reputation: 1023
This worked for me on my toolBar
//toolBar background image set based on iOS version
[[UIDevice currentDevice] systemVersion];
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
//iOS 5
UIImage *toolBarIMG = [UIImage imageNamed: @"toolBar_brown.png"];
if ([toolBar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) {
[toolBar setBackgroundImage:toolBarIMG forToolbarPosition:0 barMetrics:0];
}
} else {
//iOS 4
[toolBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolBar_brown.png"]] autorelease] atIndex:0];
}
Upvotes: 0
Reputation: 2526
To support both versions, you need to ask if there is setBackgroundImage:forBarMetrics: method.
Take a look at my blogpost about it: http://www.mladjanantic.com/setting-custom-background-for-uinavigationbar-what-will-work-on-ios5-and-ios4-too/
Upvotes: 4
Reputation: 170829
One more possible "hacky" solution is to create a custom view and insert it to UINavigationBar as a subView - may be it will still work:
UIView *backgroundView = ...
[navigationBar insertSubview:backgroundView atIndex:0];
Or check updated UINavigationBar class reference in iOS5 docs for built-in method to set custom background
Upvotes: 1