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