UITabBarController自定义一

时间:2022-07-08 06:45:09

UITabBarController自定义一

  • 首先在Appdelegate.m文件中将UITabBarController的子类设置为rootViewController,并设置其viewControllers
1.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2. // Override point for customization after application launch.
3. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
4. self.window.backgroundColor = [UIColor whiteColor];
5. [self.window makeKeyAndVisible];
6.
7. JKBaseTabBarController *baseTabBarController = [self createRootController];
8.
9. self.window.rootViewController = baseTabBarController;
10.
11.
12. return YES;
13.}
14.
15.#pragma mark - 创建跟视图
16.-(JKBaseTabBarController *)createRootController{
17. JKBaseTabBarController *baseTabBarController = [[JKBaseTabBarController alloc] init];
18.
19. // items
20.// NSArray *titles = @[@"总览",@"账户",@"图表"];
21.
22. JKGeneralViewController *general = [[JKGeneralViewController alloc] init];
23. general.tabBarItem.title = @"总览";
24. general.tabBarItem.image = [[UIImage imageNamed:@"overview_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
25. general.tabBarItem.selectedImage = [[UIImage imageNamed:@"overview_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
26.
27. JKAccountViewController *account = [[JKAccountViewController alloc] init];
28. account.tabBarItem.title = @"账户";
29. account.tabBarItem.image = [[UIImage imageNamed:@"account_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
30. account.tabBarItem.selectedImage = [[UIImage imageNamed:@"account_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
31.
32. JKGraphViewController *graph = [[JKGraphViewController alloc] init];
33. graph.tabBarItem.title = @"图表";
34. graph.tabBarItem.image = [[UIImage imageNamed:@"graph_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
35. graph.tabBarItem.selectedImage = [[UIImage imageNamed:@"graph_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
36.
37. baseTabBarController.viewControllers = @[general,account,graph];
38.
39. return baseTabBarController;
40.}
41.
42.
  • 然后在UITabBarController子类的viewWillAppear方法中将tabBar的子视图隐藏掉,换上自定义的视图
1.#pragma mark - 自定义
2.-(void)viewWillAppear:(BOOL)animated{
3. [super viewWillAppear:animated];
4. self.tabBar.barTintColor = [UIColor blackColor];
5.
6. // 先将系统自带的隐藏
7. for (UIView *view in self.tabBar.subviews) {
8. view.hidden = YES;
9. }
10.
11. NSArray *controllers = self.viewControllers;
12.
13. // 根据有多少项计算宽度
14. CGFloat h = self.tabBar.bounds.size.height;
15. CGFloat w = h;
16. CGFloat interval = (s_w - controllers.count * h) / (controllers.count + 1);
17.
18. for (int i = 0; i < controllers.count; i++) {
19. UITabBarItem *item = ((UIViewController *)controllers[i]).tabBarItem;
20. UIButton *btn = [[UIButton alloc] initWithFrame:(CGRect){interval + i * (w + interval),0,w,h}];
21.
22. btn.imageEdgeInsets = UIEdgeInsetsMake(-15, 0, 0, 0);
23.
24. [btn setImage:item.image forState:UIControlStateNormal];
25. [btn setImage:item.selectedImage forState:UIControlStateSelected];
26.
27. btn.titleEdgeInsets = UIEdgeInsetsMake(30, -35, 0, 0);
28. btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
29. [btn setTitle:item.title forState:UIControlStateNormal];
30. [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
31. [btn setTitleColor:lightBlueColor forState:UIControlStateSelected];
32.
33.
34. btn.tag = i + 200;
35. [btn addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
36.
37.
38. if (i == 0) {
39. btn.selected = YES;
40. }
41.
42. [self.tabBar addSubview:btn];
43. }
44.}
45.
46.-(void)changeViewController:(UIButton *)sender{
47. if (!sender.selected) {
48. sender.selected = !sender.selected;
49. self.selectedIndex = sender.tag - 200;
50.
51. for (UIView *view in self.tabBar.subviews) {
52. if ([view isKindOfClass:[UIButton class]] && view.tag != sender.tag) {
53. UIButton *btn = (UIButton *)view;
54. btn.selected = NO;
55. }
56. }
57. }
58.
59.}
60.
  • 效果如图
    UITabBarController自定义一
    *没有上诉自定义的话也可以通过
    [general.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} forState:UIControlStateNormal];方法改变title的颜色