改变UITabBarController的颜色

本文详细介绍了如何在iOS应用中自定义TabBar颜色,并通过贴图实现更多功能。包括使用Swift和Objective-C代码实现,以及如何在iOS 5及以上版本中直接修改颜色的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:http://blog.sina.com.cn/s/blog_49b531af0102dz6d.html


其实主这几行代码的事:

  1. UIView mView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 48)];//这是整个tabbar的颜色  
  2.     [mView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar.png"]]];  
  3.     [tab.tabBar insertSubview:mView atIndex:1];  
  4.     mView.alpha=0.8; 


ios5中已经有了修改颜色的方法、可以直接使用;

之前的方法是继承tabbarController自定义一个tabbar,可是这样的话,视图多了就没有了,像"more"那样的功能,所以直接往tabbar上面贴图


[cpp]  view plain copy
  1. <pre name="code" class="cpp"    
  2.   
  3. UITabBarController tab=[[UITabBarController alloc] init];  
  4.     tab.delegate=self;  
  5.       
  6.        
  7.       
  8.     UIViewController aView=[[UIViewController alloc] init];  
  9.     [aView.view setBackgroundColor:[UIColor redColor]];  
  10.     aView.tabBarItem.title=@"第1页" 
  11.       
  12.       
  13.     UIViewController bView=[[UIViewController alloc] init];  
  14.     [bView.view setBackgroundColor:[UIColor blueColor]];  
  15.     [bView.tabBarItem setTitle:@"第2页"];  
  16.       
  17.     UIViewController cView=[[UIViewController alloc] init];  
  18.     [cView.view setBackgroundColor:[UIColor whiteColor]];  
  19.     [cView.tabBarItem setTitle:@"第3页"];  
  20.     UIViewController dView=[[UIViewController alloc] init];  
  21.     [dView.view setBackgroundColor:[UIColor whiteColor]];  
  22.     [dView.tabBarItem setTitle:@"第4页"];     
  23.     UIViewController eView=[[UIViewController alloc] init];  
  24.     [eView.view setBackgroundColor:[UIColor whiteColor]];  
  25.     [eView.tabBarItem setTitle:@"第5页"];      
  26.     UIViewController fView=[[UIViewController alloc] init];  
  27.     [fView.view setBackgroundColor:[UIColor whiteColor]];  
  28.     [fView.tabBarItem setTitle:@"第6页"];  
  29.       
  30.     NSArray array=[[NSArray alloc] initWithObjects:aView ,bView,cView,dView,eView,fView, nil];  
  31.     tab.viewControllers=array;  
  32.     tab.selectedViewController=0;  
  33.           
  34.       
  35.     UIView mView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 48)];//这是整个tabbar的颜色  
  36.     [mView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar.png"]]];  
  37.     [tab.tabBar insertSubview:mView atIndex:1];  
  38.     mView.alpha=0.8;  



 
 
这是最直接、最简单的方法 


转自 http://blog.youkuaiyun.com/ydj213/article/details/7072714

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值