Custom UINavigationBar with image and back button
Categories: Apple, iPad, iPhone, Mac OS X, OOP on Feb.09, 2010
Many people could not find this solution! It’s really very simple solution and it does not contain any private methods and functions. It’s based on the drawRect method and the simple manipulation with the title. If the title is specified, then the text of title displays, but if not, then displays the image.
1 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 29 | @implementation UINavigationBar (UINavigationBarCustomDraw) - (void) drawRect:(CGRect)rect { [self setTintColor:[UIColor colorWithRed:0.5f green: 0.5f blue:0 alpha:1]]; if ([self.topItem.title length] > 0 && ![self.topItem.title isEqualToString:@"Back to ..."]) { [[UIImage imageNamed:@"Header_1px.png"] drawInRect:rect]; CGRect frame = CGRectMake(0, 0, 320, 44); UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease]; [label setBackgroundColor:[UIColor clearColor]]; label.font = [UIFont boldSystemFontOfSize: 20.0]; label.shadowColor = [UIColor colorWithWhite:0.0 alpha:1]; label.textAlignment = UITextAlignmentCenter; label.textColor = [UIColor whiteColor]; label.text = self.topItem.title; self.topItem.titleView = label; } else { [[UIImage imageNamed:@"Header.png"] drawInRect:rect]; self.topItem.titleView = [[[UIView alloc] init] autorelease]; } } @end |
Similar posts:

(4 votes, average: 4.75 out of 5)
September 4th, 2010 on 11:24 am
nice i was looking for that great website
November 30th, 2010 on 6:48 am
Ok, I am very new to this. Where do I put that code? Can I put that in my viewcontroller.m? Or is that a file in and of itself?
July 6th, 2011 on 9:12 am
Hi, thanks for this.
How would you implement this if you want to use the same menu in all your view controllers, not just in one?
July 6th, 2011 on 10:20 am
@Rocco it’s for all navigation bars, because implemented “UINavigationBarCustomDraw” category for UINavigationBar and rewrited drawRect
Put this code to new file h and import in prefix file for all controllers