开发者

UINavigationBar Background Image Problem

i have set my navigationbar background with this code in my App delegate:

@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
    UIImage *backgroundImage = [UIImage imageNamed: @"Nav-rightpane.pn开发者_Python百科g"];

    CGContextDrawImage(UIGraphicsGetCurrentContext(),
                       CGRectMake(0, 0, self.frame.size.width, self.frame.size.height),
                       backgroundImage.CGImage);
}
@end

This works perfect but now i must change the background to the default at UIPopoverController. Any idea for this?

Thanks


Do not use this solution. Simply delete your UINavigationBar Category and implement your own UINavigationController.

@interface XNavigationController : UINavigationController{}

and change override viewDidLoad method in implementation

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImage *img = [UIImage imageNamed: @"Nav-rightpane.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
    [imageView setFrame:self.navigationBar.bounds];
    [imageView setContentMode:UIViewContentModeScaleToFill];
    [self.navigationBar addSubview:imageView];
    [imageView release];
}

so use this navigationController when you want to change the background of navigation bar.


The correct solution pre-iOS5, as recommended by Apple engineers is to not use categories and to not use method swizzling. This is confirmed to break on iOS 5 which is very close to being released.

Instead, you should create a subclass of UINavigationBar, override the drawRect: method, and then use Interface Builder to define your navigation controller. Interface Builder will let you change the classname for the navigation bar to your custom subclass:

UINavigationBar Background Image Problem

UINavigationBar Background Image Problem

If you want to avoid Interface Builder, you can create a temporary XIB and load an instance of the navigation controller from it, then use NSKeyedArchiver to archive it to a file. Then use xxd -i <filename> on the command line to generate C-code for the raw bytes of the navigation controller. Then paste that into a source file and unarchive it when you want an instance.

It seems like a lot of work, but the WWDC videos and engineers on the forums have repeatedly stated that this is the only safe and reliable way to do what you want.

In iOS 5 this all becomes a whole lot easier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜