为UINavigationBar添加自定义背景

时间:2022-04-22
本文章向大家介绍为UINavigationBar添加自定义背景,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
	//颜色填充
//	UIColor *color = [UIColor redColor];
//	CGContextRef context = UIGraphicsGetCurrentContext();
//	CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
//	CGContextFillRect(context, rect);
//	self.tintColor = color;
	//图片填充
	UIColor *color = [UIColor colorWithRed:46.0f/255.0f green:87.0f/255.0f blue:29.0f/255.0f alpha:1.0f];
	UIImage *img	= [UIImage imageNamed: @"bg.png"];
	[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
	self.tintColor = color;
}
@end

图示: