解决5.0 以下版本自定义 navigationBar 背景图片(the backgroud of the Bar)

解决5.0 以下版本自定义 navigationBar 背景图片

原因:在5.0 以下的api 中没有提供setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics 方法

this method in  5.x  api: 

- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;

 

解决方案:

1. under the 5.0 version

1)在将要在navigation的 推出的ViewControll 中加入一个 Category

(Category 目录:你可以将它看做是一个类的方法的扩充,他只能扩充方法,不可扩充属性,一般用在 扩充 Api 中的 苹果提供的类   方便使用)

 

要添加的目录: 添加一个是一个navigationBar 一个目

@implementation UINavigationBar (CustomImage)   

- (void)drawRect:(CGRect)rect {   

    UIImage *image = [UIImageimageNamed@"nav_bg.png"];   

    [image drawInRect:CGRectMake(00self.frame.size.widthself.frame.size.height)];   

}

@end

 

ok 了 try 一下 just so easy

2. In  5.x Version

just use the method at top of this article

 

3. 如果是在混合版本下 就是 支持 4.0或者5.0的

我们可以通过两种判断 当前的ios 版本

1)--

 if ([self.navigationController.navigationBarrespondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {

        [self.navigationController.navigationBarsetBackgroundImage:[UIImageimageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];

    }

 

2)--

if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 5.0) {

        [self.navController.navigationBarsetBackgroundImage:[UIImageimageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];

    }

原文地址:https://www.cnblogs.com/zander/p/2643629.html