2

我在 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+ 的代码,请帮助我。

4

1 回答 1

3

请试试这个:

@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

于 2012-06-06T21:10:33.803 回答