修改navigation的背景和背景图片

来源:互联网 发布:mac 如何转换flac ape 编辑:程序博客网 时间:2024/06/07 17:40
  1. //通过背景图片来设置背景  
  2. float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];  
  3. UIImage *backgroundImage = [UIImage imageNamed:@"navbg.png"];  //获取图片  
  4.   
  5. if(systemVersion>=5.0)  
  6. {  
  7.     CGSize titleSize = self.navigationController.navigationBar.bounds.size;  //获取Navigation Bar的位置和大小  
  8.     backgroundImage = [self scaleToSize:backgroundImage size:titleSize];//设置图片的大小与Navigation Bar相同  
  9.     [self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];  //设置背景  
  10. }  
  11. else  
  12. {  
  13.     [self.navigationController.navigationBar insertSubview:[[[UIImageView alloc] initWithImage:backgroundImage] autorelease] atIndex:1];  
  14. }  
  15.   
  16. //调整图片大小  
  17. - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{  
  18.     UIGraphicsBeginImageContext(size);  
  19.     [img drawInRect:CGRectMake(0, 0, size.width, size.height)];  
  20.     UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();  
  21.     UIGraphicsEndImageContext();  
  22.     return scaledImage;  
  23. }  
  24.   
  25. //设置背景样式可用通过设置tintColor来设置  
  26. self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:50/255.0 green:138/255.0 blue:233/255.0 alpha:1.0];//改变navigation的背景颜色 
0 0
原创粉丝点击