Subclassing UINavigationBar and setting background image for iOS 5
Question: I’ve just updated to iOS 5 and my iPhone applications are all working properly except the custom navigation bar. The background image is not drawn. I am not sure why. I am using Xcode 4.3 SDK and my code is attached.
Answer: Since iOS 5 the UINavigationBar, UIToolbar, and UITabBar implementations have changed. The [drawRect:] method is not called on instances of these classes unless it is implemented in a subclass. Ergo, forget your previous Category solution. You must have a subclass like this:
Notes:
1. If you are getting
1 2 | *** ImageIO - could not find ColorSync function 'ColorSyncProfileCreateWithName' and *** ImageIO - could not find ColorSync function 'ColorSyncProfileCopyData' |
error messages, your image is not prepared properly. Read this post for solution.
2. What to do with subclass? Open the xib where your Navigation Controller (UINavigationController) can be found. Select Identity Inspector and change class type from UINavigationBar to myNavigationBar. Save changes.
Comments
Log in to post (or reply to) a comment. You can use your Twitter account.
This blog was originally designed for our own project partners and freelance programmers to share questions and solutions in this online private pub. From April 1, 2011 we open the blog for all of you to add comments or ask a question in a new post. All comments and posts are moderated before they are posted to verify the quality of content on our pages. Comments are usually posted within 15 minutes. Posts may be re-edited. You can post source codes in comments using [code][/code] tag.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
}
@end
@implementation myNavigationBar
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
// ColorSync manipulated image
UIImage *imageBackground = [UIImage imageNamed: @"navback.png"];
[imageBackground drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) ];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
}
@end