简体   繁体   English

UINavigationBar背景图像问题

[英]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.png"];

    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. 这工作完美,但现在我必须将UIPopoverController的背景更改为默认值。 Any idea for this? 有什么想法吗?

Thanks 谢谢

Do not use this solution. 不要使用此解决方案。 Simply delete your UINavigationBar Category and implement your own UINavigationController . 只需删除您的UINavigationBar类别并实现您自己的UINavigationController

@interface XNavigationController : UINavigationController{}

and change override viewDidLoad method in implementation 并在实现中更改重写viewDidLoad方法

- (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. 因此,当您要更改导航栏的背景时,请使用此navigationController

The correct solution pre-iOS5, as recommended by Apple engineers is to not use categories and to not use method swizzling. Apple工程师建议,iOS5之前的正确解决方案是不要使用类别并且不要使用方法混乱。 This is confirmed to break on iOS 5 which is very close to being released. 已确认这将在即将发布的iOS 5上中断。

Instead, you should create a subclass of UINavigationBar , override the drawRect: method, and then use Interface Builder to define your navigation controller. 相反,您应该创建UINavigationBar的子类,重写drawRect:方法,然后使用Interface Builder定义导航控制器。 Interface Builder will let you change the classname for the navigation bar to your custom subclass: Interface Builder将使您可以将导航栏的类名更改为自定义子类:

在此处输入图片说明

在此处输入图片说明

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. 如果要避免使用Interface Builder,则可以创建一个临时XIB并从中加载导航控制器的实例,然后使用NSKeyedArchiver将其归档到文件中。 Then use xxd -i <filename> on the command line to generate C-code for the raw bytes of the navigation controller. 然后,在命令行上使用xxd -i <filename>为导航控制器的原始字节生成C代码。 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. 这似乎是一项艰巨的工作,但是WWDC视频和论坛上的工程师反复指出,这是唯一安全可靠的方法来完成您想要的事情。

In iOS 5 this all becomes a whole lot easier. 在iOS 5中,这一切变得非常容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM