简体   繁体   English

UINavigationBar-设置背景图像?

[英]UINavigationBar - Setting background image?

So I have a category for UINavigationController and I override the init method as below 所以我有一个UINavigationController的类别,并且重写了init方法,如下所示

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
            UIImage *image = [UIImage imageNamed:@"navBar.png"];
            [[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];      
    }

        return self;
}

The following code causes the background image of the navigation bar to become blank (white). 以下代码使导航栏的背景图像变为空白(白色)。 The image name is correct and it exists within the project. 图像名称正确,并且存在于项目中。 Any idea what is causing this problem? 知道是什么引起了这个问题吗?

If you use iOS4, you can override - (void)drawRect 如果您使用iOS4,则可以覆盖-(void)drawRect

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect
{
    UIImage *navBg = [UIImage imageNamed:@"navBar.png"];
    [navBg drawInRect:rect];
}
@end

iOS 5, iOS 5

if ([[UIDevice currentDevice].systemVersion floatValue] >= 5.0) {
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){

        UIImage *navBarImg = [UIImage imageNamed:@"navBar.png"];

        [self.navigationController.navigationBar setBackgroundImage:navBarImg forBarMetrics:UIBarMetricsDefault];

    }
}

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

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