UINavigationBar - 设置背景图片? [英] UINavigationBar - Setting background image?

查看:189
本文介绍了UINavigationBar - 设置背景图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个UINavigationController的类别,我重写了以下的init方法

So I have a category for UINavigationController and I override the init method as below

- (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?

推荐答案

如果使用iOS4,你可以覆盖 - (void)drawRect

If you use iOS4, you can override - (void)drawRect

@implementation UINavigationBar (UINavigationBarCategory)

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

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];

    }
}

这篇关于UINavigationBar - 设置背景图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆