iphone - 将导航栏背景更改为图像

标签 iphone

我在 IOS 4+ 上尝试了这段代码,它运行得很好,但直到我的队友在 IOS 5+ 上测试了它。这是代码

@implementation UINavigationBar (CustomImage)

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

@end

我需要一个适用于 IOS 4+ 和 IOS 5+ 的代码,请帮助我。

最佳答案

请尝试这个:

@implementation UINavigationBar (CustomImage)

// Set bar background
- (UIImage *)customBackground {
    UIImage *image = [UIImage imageNamed:@"background.png"];
    return image;
}

- (void)didMoveToSuperview {
    // Applies to iOS5 only
    if ([self respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        [self setBackgroundImage:[self customBackground] forBarMetrics:UIBarMetricsDefault];
    }
}

// Set the navigation bar background
- (void)drawRect:(CGRect)rect {
    [[self customBackground] drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

setBackgroundImage:forBarMetrics: 适用于 iOS5

关于iphone - 将导航栏背景更改为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10922297/

相关文章:

iphone - 从 NSDate 获取时间返回 nil

iphone - 如何截断 UILabel 中字符串中的字符串?

javascript - 将 objective-c 变量传递给 ios 中的 javascript

ios - 当我滑动 uitableviewcell 时调用 didSelectRowAtIndexPath

iphone - UItextView 文本隐藏

iphone - iPhone:如何从JSON请求获取地址?

c++ - 在 iOS 中生成 AcoustID/Chromaprint 声学指纹

iphone - 从后台运行应用程序时重新启动出现启动画面问题的应用程序

iphone - 翻转上下文上的 CGBitmapContextCreateImage

ios - 要求用户使用 swift2 从照片库或相机中选择照片