佳为好友

转:如何为UINavigationBar设置背景图片

10-3-12

转:http://blog.csdn.net/wave_1102/archive/2009/11/04/4768212.aspx


如何为UINavigationBar设置背景图片

在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. ......  
 

实现的效果如下图:

带有背景图片的导航条


+++++

posted on 2012-12-26 10:09 佳为好友 阅读(121) 评论(0)  编辑 收藏 引用 所属分类: UI


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2012年12月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

留言簿(1)

随笔分类

搜索

最新评论

评论排行榜