iOS中 UITabBarController中自定义UITabBar

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

 AViewController  *aa = [[AViewController alloc] init];
    UINavigationController* ayNav = [[UINavigationController alloc]initWithRootViewController:aa];

   BViewController  *bb = [[BViewController alloc] init];
   UINavigationController* bNav = [[UINavigationController alloc]initWithRootViewController:bb];

    CViewController  *cc = [[CViewController alloc] init];
    UINavigationController* cNav = [[UINavigationController alloc]initWithRootViewController:cc];

    DViewController  *dd = [[DViewController alloc] init];
    UINavigationController* dNav = [[UINavigationController alloc]initWithRootViewController:dd];
    2.初始化tabbar
   UITabBarController *tabBarController = [[UITabBarController alloc]init];
   tabBarController.delegate=self;
    tabBarController.viewControllers=[[NSArray alloc]initWithObjects:ayNav,bNav,cNav,dNav,nil];

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

UITabBar *tabBar = tabBarController.tabBar;
 UITabBarItem *aTabBarItem = [tabBar.items objectAtIndex:0];
 UITabBarItem *bTabBarItem = [tabBar.items objectAtIndex:1];
UITabBarItem *cTabBarItem = [tabBar.items objectAtIndex:2];
UITabBarItem *dTabBarItem = [tabBar.items objectAtIndex:3];

  4. 设置tabBar中items的标题

aTabBarItem.title = @"小韩哥";
bTabBarItem.title = @"iOS编程";
cTabBarItem.title = @"ios开发";
dTabBarItem.title = @"苹果系统";

   5.设置tabBar中items的图片

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

    6.设置tabBar的背景图片

 UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar1.png"];
 [[UITabBar appearance] setBackgroundImage:[tabBarBackground resizableImageWithCapInsets:UIEdgeInsetsZero]];
 [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar.png"]];


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

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                   [UIColor grayColor], UITextAttributeTextColor,
                                                   nil nil] forState:UIControlStateNormal];
 UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];
 [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                    titleHighlightedColor, UITextAttributeTextColor,
                                                   nil nil] forState:UIControlStateHighlighted];

 8.将tabBarController加入window中

self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];



  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩俊强

奖励一杯咖啡☕️

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值