现在的位置: 首页 > 综合 > 正文

如何为UINavigationBar设置背景图片

2013年10月21日 ⁄ 综合 ⁄ 共 2061字 ⁄ 字号 评论关闭

在iPhone开发中, 有时候我们想给导航条添加背景图片, 实现多样化的导航条效果, 用其他方法往往无法达到理想的效果, 经过网上搜索及多次实验, 确定如下最佳实现方案:

为UINavigatonBar增加如下Category:

  1. @implementation UINavigationBar (CustomImage)  
  2. - (void
    )drawRect:(CGRect)rect {  
  3.     UIImage *image = [UIImage imageNamed: @"NavigationBar.png"
    ];  
  4.     [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];  
  5. }  
  6. @end  

 

例如, 在我的项目中, 添加如下代码:

  1. /////////////////////////////////////////////////////////
      
  2. /* input: The image and a tag to later identify the view */
      
  3. @implementation UINavigationBar (CustomImage)  
  4. - (void
    )drawRect:(CGRect)rect {  
  5.     UIImage *image = [UIImage imageNamed: @"title_bg.png"
    ];  
  6.     [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];  
  7. }  
  8. @end  
  9. /////////////////////////////////////////////////////////
      
  10. @implementation FriendsPageViewController  
  11. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
      
  12. - (void
    )viewDidLoad {     
  13.     self.navigationBar.tintColor = [UIColor purpleColor];  
  14.       
  15.     [self initWithRootViewController:[[RegPageViewController alloc] init]];  
  16.     [super viewDidLoad];  
  17. }  
  18. ......  

 

实现的效果如下图:

带有背景图片的导航条

抱歉!评论已关闭.