Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom background for UINavigationBar in landscape mode

I am adding a custom background for my UINavigationBar. It works fine as long as the phone is in portrait mode. As soon as I switch to landscape mode, half the bar appears blue (the default navbar color) and half of it has my image

How can I stretch the image for landscape mode and make it small again for portrait mode?

Thanks

Solution
Incase anyone is looking for an answer to how to add an image to navigation bar - here goes

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 480.0, 44.0)];
[imgView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"navbar_landscape" ofType:@"png"]]];
[navigationController.navigationBar addSubview:imgView];
[imgView release];
like image 238
lostInTransit Avatar asked Dec 19 '08 09:12

lostInTransit


2 Answers

In both screen orientation modes it's much better to use

[navigationController.navigationBar insertSubview:imgView atIndex:0];

This puts image view under all other views and all the default navigation bar elements (title, standard buttons) work OK.

like image 173
Ivan Karpan Avatar answered Sep 20 '22 00:09

Ivan Karpan


After a bit of research and trail and error, I found a work around, that will not replace the navbar when you enter the movie playback mode. Hopeefully this does not cause problems with app approval, but based on this post, I think it should be fine:

http://developer.apple.com/iphone/library/qa/qa2009/qa1637.html

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    if([self isMemberOfClass: [UINavigationBar class]]){
        UIImage *image = [UIImage imageNamed:@"navBarBackground.png"];
        CGContextClip(ctx);
        CGContextTranslateCTM(ctx, 0, image.size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextDrawImage(ctx, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
    }else{
        [super drawLayer:layer inContext:ctx];
    }
}

@end
like image 28
ljonesATL Avatar answered Sep 20 '22 00:09

ljonesATL