CustomTabBarController.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // BPViewController.m
  3. // 家有宝贝
  4. //
  5. // Created by wuli萍萍 on 16/5/21.
  6. // Copyright © 2016年 wuli萍萍. All rights reserved.
  7. //
  8. #import "CustomTabBarController.h"
  9. #import "HomePageVC.h"
  10. #import "MallHomeVC.h"
  11. #import "MineHomeVC.h"
  12. #import "CustomTabBar.h"
  13. #import "xingchuangke-Swift.h"
  14. @interface CustomTabBarController ()
  15. @end
  16. @implementation CustomTabBarController
  17. #pragma mark 懒加载
  18. - (void)viewWillAppear:(BOOL)animated {
  19. [super viewWillAppear:animated];
  20. //去掉分割线
  21. GB_Nav.navigationBar.shadowImage = [UIImage new];
  22. }
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. self.delegate = self;
  26. [self.view setBackgroundColor:COLOR_BACKGROUND];
  27. // 添加子控制器
  28. [self setUpChildVC:[HomePageVC new] title:@"首页" image:@"home_un" selectedImage:@"home"];
  29. [self setUpChildVC:[ShoppingMallCollectionViewController new] title:@"商城" image:@"aide_un" selectedImage:@"aide"];
  30. [self setUpChildVC:[MyViewController new] title:@"我的" image:@"mine_un" selectedImage:@"mine"];
  31. // 设置tabbar的背景图片
  32. [[CustomTabBar appearance]setBackgroundColor:[UIColor whiteColor]];
  33. [[CustomTabBar appearance]setShadowImage:[[UIImage alloc]init]];//将TabBar上的黑线去掉
  34. CustomTabBar *tabBar = [[CustomTabBar alloc] init];
  35. // 设置代理
  36. tabBar.delegate = self;
  37. [self setValue:tabBar forKey:@"tabBar"];
  38. // [self setupTabBar];
  39. }
  40. - (void)setupTabBar{
  41. UIView *bgView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
  42. bgView.backgroundColor = [UIColor whiteColor];
  43. //[self.tabBar insertSubview:bgView atIndex:0];
  44. self.tabBar.opaque = YES;
  45. }
  46. #pragma mark ---- 初始化子控制器
  47. - (void)setUpChildVC:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
  48. vc.title = title;
  49. vc.tabBarItem.image = [[UIImage imageNamed:image]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  50. // 禁用图片渲染
  51. vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  52. //设置字体和图片的距离
  53. CGFloat spaceTop;
  54. CGFloat imageTop;
  55. CGFloat fontSize;
  56. if (SCREEN_WIDTH == 320) {
  57. imageTop = -2;
  58. fontSize = 9;
  59. spaceTop = -4;
  60. }else if (SCREEN_WIDTH == 375){
  61. imageTop = -2;
  62. fontSize = 11;
  63. spaceTop = -1;
  64. }else if (SCREEN_WIDTH == 414){
  65. imageTop = -2;
  66. fontSize = 12;
  67. spaceTop = -5;
  68. }else{
  69. imageTop = -2;
  70. spaceTop = -5;
  71. fontSize = 11;
  72. }
  73. [vc.tabBarItem setImageInsets:UIEdgeInsetsMake(imageTop,0, -imageTop, 0)];
  74. [vc.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, spaceTop)];
  75. // if (@available(iOS 13.0, *)) {
  76. // UITabBarAppearance *appearance = [self.tabBar.standardAppearance copy];
  77. // // 设置未被选中的颜色
  78. // appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName :COLOR_TEXT_UNSELECT,NSFontAttributeName:[UIFont systemFontOfSize:F(12)]};
  79. // // 设置被选中时的颜色
  80. // appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName :[UIColor blackColor] ,NSFontAttributeName:[UIFont systemFontOfSize:F(12)]};
  81. // appearance.shadowColor = [UIColor clearColor];
  82. // [appearance configureWithTransparentBackground];
  83. //// if ((SCREEN_HEIGHT == 812||SCREEN_HEIGHT == 896)) {
  84. //// appearance.backgroundImage=[UIImage imageNamed:@"background_tab_375"];
  85. //// }else{
  86. //// appearance.backgroundImage=[UIImage imageNamed:@"background_tab"];
  87. //// }
  88. // vc.tabBarController.tabBar.standardAppearance = appearance;
  89. //
  90. // }else{
  91. // 设置文字的样式
  92. [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName :COLOR_TEXT_UNSELECT,NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} forState:UIControlStateNormal];
  93. [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName :[UIColor blackColor] ,NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} forState:UIControlStateSelected];
  94. self.tabBar.backgroundColor = [UIColor clearColor];
  95. // if ((SCREEN_HEIGHT == 812||SCREEN_HEIGHT == 896)) {
  96. // self.tabBar.backgroundImage=[UIImage imageNamed:@"background_tab_375"];
  97. // }else if (SCREEN_WIDTH==414){
  98. // self.tabBar.backgroundImage=[UIImage imageNamed:@"background_tab_414"];
  99. // } else{
  100. // self.tabBar.backgroundImage=[UIImage imageNamed:@"background_tab"];
  101. // }
  102. [self.tabBar setShadowImage:[UIImage new]];//将TabBar上的黑线去掉
  103. // }
  104. // if([UINavigationBar conformsToProtocol:@protocol(UIAppearanceContainer)]) {
  105. // [UINavigationBar appearance].tintColor = [UIColor whiteColor];
  106. // [[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName : COLOR_LABEL}];
  107. // [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
  108. // [[UINavigationBar appearance] setTranslucent:NO];
  109. // }
  110. // 包装一个导航控制器, 添加导航控制器为tabbarcontroller的子控制器
  111. [self addChildViewController:vc];
  112. }
  113. - (void)viewWillLayoutSubviews
  114. {
  115. [super viewWillLayoutSubviews];
  116. CGFloat tabberHeight = TABBAR_HEIGHT;
  117. // if (SCREEN_WIDTH == 320){
  118. // tabberHeight = 50;
  119. // }else if(SCREEN_WIDTH == 375){
  120. // tabberHeight = IS_iPhoneX ? 83 : 53;
  121. // }else{
  122. // tabberHeight = 55;
  123. // }
  124. CGRect tabFrame = self.tabBar.frame; //self.TabBar is IBOutlet of your TabBar
  125. tabFrame.size.height = tabberHeight;
  126. tabFrame.origin.y = self.view.frame.size.height - tabberHeight;
  127. self.tabBar.frame = tabFrame;
  128. }
  129. #pragma mark tabbarVC delegate
  130. -(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
  131. return true;
  132. }
  133. @end