ios ui开发过程中,经常会使用到导航栏,默认的样式比较单一,所以经常需要修改导航栏的样式
ios4:
class="brush:objc;gutter:true;">- (void)drawRect:(CGRect)rect { UIImage *image = [UIImage imageNamed:@"navbar_bg.png"]; [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; return; }
ios5以后:
if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{ [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_bg.png"] forBarMetrics:UIBarMetricsDefault]; }
ios7,由于导航条会扩展到系统状态栏上面,所以图片高度需要增加20px,如果没有另外处理可能会出现状态栏黑条
if (7.0 <= [[[UIDevice currentDevice] systemVersion] floatValue]) { [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_bg_7.png"] forBarMetrics:UIBarMetricsDefault]; } else { [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_bg.png"] forBarMetrics:UIBarMetricsDefault]; }