CTTabbarController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // CTTabbarController.m
  3. // wct_tabbar
  4. //
  5. // Created by Wcting on 2019/4/24.
  6. // Copyright © 2019年 EJIAJX_wct. All rights reserved.
  7. //
  8. #import "CTTabbarController.h"
  9. #import "ViewController.h"
  10. #import "UINavigationController+FDFullscreenPopGesture.h"
  11. @interface CTTabbarController ()
  12. @property (nonatomic, strong)NSArray *arrTabImage;
  13. @property (nonatomic, strong)UIButton *buttonCurrent; //保存当前选中的按钮
  14. @property (nonatomic, assign)NSInteger selectIndex;
  15. @end
  16. static CTTabbarController *tabbarController = nil;
  17. @implementation CTTabbarController
  18. +(CTTabbarController *)sharaTabbarController
  19. {
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. tabbarController = [[CTTabbarController alloc] init];
  23. });
  24. return tabbarController;
  25. }
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. // Do any additional setup after loading the view.
  29. // self.view.backgroundColor = [UIColor redColor];
  30. [self currentController];
  31. }
  32. - (void)currentController
  33. {
  34. //首页 - 使用新版首页
  35. self.vc1 = [[NewHomeViewController alloc] init];
  36. //短剧
  37. self.vc2 = [[JXShortDramaViewController alloc] init];
  38. // 暂时注释掉影视(搜索)页签
  39. // self.vc3 = [[SearchViewController alloc] init];
  40. //我的
  41. self.vc4 = [[MineViewController alloc] init];
  42. GDNavigationController *nav1 = [[GDNavigationController alloc] initWithRootViewController:self.vc1];
  43. GDNavigationController *nav2 = [[GDNavigationController alloc] initWithRootViewController:self.vc2];
  44. // UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:self.vc3]; // 暂时注释
  45. GDNavigationController *nav4 = [[GDNavigationController alloc] initWithRootViewController:self.vc4];
  46. [nav1 setNavigationBarHidden:YES animated:NO];
  47. [nav2 setNavigationBarHidden:YES animated:NO];
  48. // [nav3 setNavigationBarHidden:YES animated:NO]; // 暂时注释
  49. [nav4 setNavigationBarHidden:YES animated:NO];
  50. NSArray *arrayVC = @[nav1, nav2, nav4]; // 首页、短剧、我的
  51. [self setViewControllers:arrayVC];
  52. for(int i=0;i<self.arrTabImage.count;i++){
  53. if(arrayVC[i]){
  54. UIViewController *controller = arrayVC[i];
  55. controller.title=self.arrTabImage[i][@"t"];
  56. UITabBarItem *item=[[UITabBarItem alloc] initWithTitle:self.arrTabImage[i][@"t"] image:[[UIImage imageNamed:self.arrTabImage[i][@"n"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:self.arrTabImage[i][@"s"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  57. item.tag=100+i;
  58. controller.tabBarItem=item;
  59. }
  60. }
  61. }
  62. +(void)initialize {
  63. UIColor *xcolor = rgba(28, 35, 61, 0.9);
  64. NSDictionary *attrNormal = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName:rgba(255, 255, 255, 0.6)};
  65. NSDictionary *attrSelect = [NSDictionary dictionary];
  66. UITabBar *tabBar = [UITabBar appearance];
  67. //ios 13 之后需要这样设置才有效
  68. if (@available(iOS 13.0, *)) {
  69. attrSelect = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName:UIColorWithRGB(0xFFD400)};
  70. UITabBarAppearance *tabBarAppearance = [[UITabBarAppearance alloc]init];
  71. //设置tabar背景色
  72. tabBarAppearance.backgroundColor = xcolor;
  73. tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = attrNormal;
  74. tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = attrSelect;
  75. //必须要加上这两句
  76. tabBar.standardAppearance = tabBarAppearance;
  77. if (@available(iOS 15.0, *)) {
  78. tabBar.scrollEdgeAppearance = tabBarAppearance;
  79. } else {
  80. // Fallback on earlier versions
  81. }
  82. } else {
  83. attrSelect = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName:UIColorWithRGB(0xFFD400)};
  84. UITabBarItem *tbItem = [UITabBarItem appearance];
  85. [tbItem setTitleTextAttributes:attrNormal forState:UIControlStateNormal];
  86. [tbItem setTitleTextAttributes:attrSelect forState:UIControlStateSelected];
  87. [tabBar setBarTintColor:xcolor]; //tabBar的背景色
  88. }
  89. tabBar.translucent = YES; //translucent: 半透明的
  90. }
  91. - (UIImage *)imageWithColor:(UIColor *)color
  92. {
  93. CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  94. UIGraphicsBeginImageContext(rect.size);
  95. CGContextRef context = UIGraphicsGetCurrentContext();
  96. CGContextSetFillColorWithColor(context, [color CGColor]);
  97. CGContextFillRect(context, rect);
  98. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  99. UIGraphicsEndImageContext();
  100. return image;
  101. }
  102. -(void)dealloc{
  103. }
  104. -(void)viewDidAppear:(BOOL)animated{
  105. [super viewDidAppear:animated];
  106. }
  107. -(void)viewWillAppear:(BOOL)animated{
  108. [super viewWillAppear:animated];
  109. }
  110. -(NSArray *)arrTabImage
  111. {
  112. if (_arrTabImage == nil) {
  113. _arrTabImage = [NSArray arrayWithObjects:
  114. @{@"n":@"icon-2.1",@"s":@"icon-1.1",@"t":@"首页"},
  115. @{@"n":@"icon-2.2",@"s":@"icon-1.2",@"t":@"短剧"},
  116. // @{@"n":@"Frame 3",@"s":@"Frame 8",@"t":@"搜索"}, // 暂时注释
  117. @{@"n":@"icon-2.4",@"s":@"icon-1.4",@"t":@"我的"},
  118. nil];
  119. }
  120. return _arrTabImage;
  121. }
  122. @end