如何为UINavigationBar设置背景图片

来源:互联网 发布:美工属于什么职位类别 编辑:程序博客网 时间:2024/05/01 11:52

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

为UINavigatonBar增加如下Category:

view plaincopy to clipboardprint?
  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  

 

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

view plaincopy to clipboardprint?
  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. ......  

 

实现的效果如下图:

带有背景图片的导航条

原创粉丝点击