简体   繁体   English

iOS导航栏backgroundimage

[英]ios navigation bar backgroundimage

I have the following code that is supposed to set the background image on my navigation bar. 我有以下代码,应该在导航栏上设置背景图像。

    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] )
    {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigation-bar-background"] forBarMetrics:UIBarMetricsDefault];
    }
    else
        NSLog(@"WTF");

It works on the iPad but not on an iPhone, on the iPhone it logs WTF ..... same result without the if statement. 它可以在iPad上运行,但不能在iPhone上运行,在iPhone上它记录WTF .....的结果相同,但没有if语句。 The iPhone I have is iPhone 4 running ios 6.0, the iPad is iPad 2 with ios 6.0 我拥有的iPhone是运行ios 6.0的iPhone 4,iPad是运行ios 6.0的iPad 2

Any help is appreciated. 任何帮助表示赞赏。

Thanks, 谢谢,

You could try this alternate old method 您可以尝试这种替代的旧方法

@implementation UINavigationBar (CustomImage)

- (void)drawRect:(CGRect)rect {
   UIImage *image = [UIImage imageNamed: @"navigation-bar-background.png"];
   [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

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

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