iOS中 UITabBarController中自定义UITabBar

本文介绍如何在iOS应用中使用UITabBarController创建多个视图,并设置各个选项卡的标题、图片及字体颜色等。通过示例代码,读者可以学习如何初始化并配置TabBar,实现美观且功能丰富的底部导航栏。

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

1.创建多个视图控制器,放如UITabBarController中

[objc]  view plain  copy
  1. AViewController  *aa = [[AViewController alloc] init];  
  2.    UINavigationController* ayNav = [[UINavigationController alloc]initWithRootViewController:aa];  
  3.   
  4.   BViewController  *bb = [[BViewController alloc] init];  
  5.   UINavigationController* bNav = [[UINavigationController alloc]initWithRootViewController:bb];  
  6.   
  7.    CViewController  *cc = [[CViewController alloc] init];  
  8.    UINavigationController* cNav = [[UINavigationController alloc]initWithRootViewController:cc];  
  9.   
  10.    DViewController  *dd = [[DViewController alloc] init];  
  11.    UINavigationController* dNav = [[UINavigationController alloc]initWithRootViewController:dd];  
  12.    2.初始化tabbar  
  13.   UITabBarController *tabBarController = [[UITabBarController alloc]init];  
  14.   tabBarController.delegate=self;  
  15.    tabBarController.viewControllers=[[NSArray alloc]initWithObjects:ayNav,bNav,cNav,dNav,nil];  

  3.获取到tabBarController中的tabBar,在从tabBar中获取到每个items

[objc]  view plain  copy
  1. UITabBar *tabBar = tabBarController.tabBar;  
  2.  UITabBarItem *aTabBarItem = [tabBar.items objectAtIndex:0];  
  3.  UITabBarItem *bTabBarItem = [tabBar.items objectAtIndex:1];  
  4. UITabBarItem *cTabBarItem = [tabBar.items objectAtIndex:2];  
  5. UITabBarItem *dTabBarItem = [tabBar.items objectAtIndex:3];  

  4. 设置tabBar中items的标题

[objc]  view plain  copy
  1. aTabBarItem.title = @"小韩哥";  
  2. bTabBarItem.title = @"iOS编程";  
  3. cTabBarItem.title = @"ios开发";  
  4. dTabBarItem.title = @"苹果系统";  

   5.设置tabBar中items的图片

[objc]  view plain  copy
  1. [aTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"aa_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"1.png"]];  
  2. [bTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"bb_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"2.png"]];  
  3. [cTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"cc_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"3.png"]];  
  4. [dTabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"dd_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"4.png"]];  

    6.设置tabBar的背景图片

[objc]  view plain  copy
  1. UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar1.png"];  
  2. [[UITabBar appearance] setBackgroundImage:[tabBarBackground resizableImageWithCapInsets:UIEdgeInsetsZero]];  
  3. [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar.png"]];  


   7.改变tabBar中items上字体的颜色

[objc]  view plain  copy
  1. [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:  
  2.                                                    [UIColor grayColor], UITextAttributeTextColor,  
  3.                                                    nil nil nil] forState:UIControlStateNormal];  
  4.  UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];  
  5.  [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:  
  6.                                                     titleHighlightedColor, UITextAttributeTextColor,  
  7.                                                    nil nil nil] forState:UIControlStateHighlighted];  

 8.将tabBarController加入window中

[objc]  view plain  copy
  1. self.window.rootViewController = tabBarController;  
  2. [self.window makeKeyAndVisible];  





原文地址:http://blog.youkuaiyun.com/qq_31810357/article/details/49833581

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值