自定义NavigationBar的背景(兼容ios4)

iOS4

在AppDelegate.m的文件中加入如下定义,覆盖NavigationBar的drawRect函数:

  1. @interface UINavigationBar (CustomBackgroud)  
  2.   
  3. - (void)drawRect:(CGRect)rect;  
  4.   
  5. @end  
  6.   
  7. @implementation UINavigationBar (CustomBackgroud)  
  8.   
  9. // iOS 4.* 下替换导航条皮肤  
  10. - (void)drawRect:(CGRect)rect  
  11. {  
  12.     UIImage *navBarImg = [[UIImage imageNamed:@"nav_bar"] stretchableImageWithLeftCapWidth:1 topCapHeight:0.0];  
  13.     [navBarImg drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];  
  14. }  
  15.   
  16. @end 

iOS5

[navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav.png"] forBarMetrics:UIBarMetricsDefault]; 


你可能感兴趣的:(ios)