Custom UINavigationBar with image and back button

Posted by – September 27, 2011

@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

source: http://www.developers-life.com/custom-uinavigationbar-with-image-and-back-button.html

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>