1

I am using the following code to add a custom navbar to my app. But I want to be able to change the navbar depending on the view, is this possible?

@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect 
{
    UIColor *color = [UIColor colorWithRed:82/255.0 green:80/255.0 blue:81/255.0 alpha:1.0];
    UIImage *img  = [UIImage imageNamed: @"navbar.png"];
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.tintColor = color;
}

@end

If not, is it possible to just add an image to the navbar instead of title text?

1 Answers1

2

I would strongly discourage using a category to override -drawRect on a UINavBar as this design will break on a soon to be released iOS version.

It is very easy to create custom images and buttons to the navBar title though. Here's some code snippets I pulled directly out of production code for one of my apps. It creates a custom image button in the navBar's title area, but it's just as easy to create an image instead:

- (void) viewDidLoad {
    UIButton *titleButton = [self buttonForTitleView];
    [titleButton setTitle:newTitle forState:UIControlStateNormal];
    [titleButton sizeToFit];

    self.navigationBar.topItem.titleView = titleButton;

}

- (UIButton *) buttonForTitleView {
    if (!_buttonForTitleView) {
        UIImage *buttonImage = [UIImage imageNamed:@"button_collection_name2"];
        UIImage *pressedButtonImage = [UIImage imageNamed:@"button_collection_name2_pressed"];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage: buttonImage forState : UIControlStateNormal];
        [button setBackgroundImage: pressedButtonImage forState : UIControlStateHighlighted];
        [button setFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)];
        [button setTitleColor:[UIColor colorWithRed:38.0/255.0 green:38.0/255.0 blue:38.0/255.0 alpha:1.0] forState:UIControlStateNormal];
        [button setTitleShadowColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
        button.titleLabel.adjustsFontSizeToFitWidth = YES;
        button.titleLabel.minimumFontSize = 10.0;
        button.titleEdgeInsets = UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0);
        button.titleLabel.shadowOffset = (CGSize){ 0, 1 };
        [button addTarget:self action:@selector(presentWidgetCollectionSwitchingViewController:) forControlEvents:UIControlEventTouchUpInside];
        _buttonForTitleView = [button retain];
    }    
    return _buttonForTitleView;
}

And this is what the button looks like in the app VideoBot: VideoBot custom navbar title button

Here's an example of using an image for the navbar title:

UIImage  *titleImage = [UIImage imageNamed:@"navbar_title_image"];
UIImageView *titleImageView = [[[UIImageView alloc] initWithImage:titleImage] autorelease];

UIView *container = [[[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, titleImage.size.width, titleImage.size.height}] autorelease];
container.backgroundColor = [UIColor clearColor];
[container addSubview:titleImageView];

// add the view to the title
self.navigationBar.topItem.titleView= container;
memmons
  • 40,222
  • 21
  • 149
  • 183
  • Thanks, in 1 view, I want a custom navbar that has an image on it instead of text (I can use a .png for this). In the other, I just want the custom navbar with the standard text, how can I do that? –  Aug 01 '11 at 23:46
  • You can put almost anything you want in the container view -- buttons, images, segmented controls. I added a UIButton to the container control, but you can add a UIImageView just as easily. – memmons Aug 02 '11 at 01:46
  • Not sure what you mean -- without being able to see your code, you'll have to debug why you are seeing odd behaviors. – memmons Aug 02 '11 at 02:17
  • @Answerbot Do we need to repeat that code for all the pages ? I mean for all the viewControllers, Where I want that kind of navigation bar? – Rajan Balana Feb 08 '13 at 11:52
  • @RajanBalana Yes. But as of iOS 5.0 we can use the appearance proxy to modify the appearance of controls throughout the app. Here's an example of using the proxy: http://stackoverflow.com/a/9813223/257550 – memmons Feb 08 '13 at 16:26