备注:之前都是通过分类修改navigationBar(添加背景图片),然后修改 tinColor
今天看到了一个新的方法 ,记录下,以备用之。
self.navigationController.navigationBar.layer.contents = (id)[UIImage imageWithColor:color].CGImage;
//根据颜色返回图片
+(UIImage*) imageWithColor:(UIColor*)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//使用分类方法
UINavigationBar 分类
#import <Foundation/Foundation.h>
@interface UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect;
@end
#import "UINavigationBarCategory.h"
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"navBar_withoutTitle.png"]; //背景图片
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end