NewBasicTabbarController.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // NewBasicTabbarController.m
  3. // MingMen
  4. //
  5. // Created by 罗云飞 on 2017/3/9.
  6. // Copyright © 2017年 罗云飞. All rights reserved.
  7. //
  8. #import "NewBasicTabbarController.h"
  9. #import "HomeRootVC.h"
  10. #import "PersonalRootVC.h"
  11. #import "DateRootVC.h"
  12. #import "FindRootVC.h"
  13. @interface NewBasicTabbarController ()<UITabBarControllerDelegate>
  14. @end
  15. static NewBasicTabbarController *sharedObj = nil;
  16. static dispatch_once_t onceToken;
  17. @implementation NewBasicTabbarController
  18. + (NewBasicTabbarController *) sharedInstance
  19. {
  20. //static dispatch_once_t onceToken = 0;
  21. dispatch_once(&onceToken, ^{
  22. sharedObj = [[super allocWithZone: NULL] init];
  23. });
  24. return sharedObj;
  25. }
  26. + (id) allocWithZone:(struct _NSZone *)zone
  27. {
  28. return [self sharedInstance];
  29. }
  30. - (id) copyWithZone:(NSZone *) zone
  31. {
  32. return self;
  33. }
  34. +(void)objectDealloc{
  35. // 只有置成0,GCD才会认为它从未执行过.它默认为0.这样才能保证下次再次调用shareInstance的时候,再次创建对象.
  36. // 应用场景:切换账号、从新登陆
  37. onceToken = 0;
  38. sharedObj = nil;
  39. }
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. // Do any additional setup after loading the view.
  43. [self loadsView];
  44. self.tabBar.barTintColor = [UIColor whiteColor];
  45. //self.tabBar.translucent = YES;//设置不透明背景
  46. /*
  47. //更改tabBar顶部线条颜色
  48. CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, 1);
  49. UIGraphicsBeginImageContext(rect.size);
  50. CGContextRef context = UIGraphicsGetCurrentContext();
  51. CGContextSetFillColorWithColor(context,
  52. NewNavigationColor.CGColor);
  53. CGContextFillRect(context, rect);
  54. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  55. UIGraphicsEndImageContext();
  56. [self.tabBar setShadowImage:img];
  57. [self.tabBar setBackgroundImage:[[UIImage alloc]init]];
  58. */
  59. }
  60. // 初始化所有子控制器
  61. - (void)loadsView{
  62. [self setTabBarChildController:[[HomeRootVC alloc] init] title:@"首页" image:@"菜单栏-首页" selectImage:@"菜单栏-首页press"];
  63. [self setTabBarChildController:[[DateRootVC alloc] init] title:@"约会" image:@"菜单栏-约会" selectImage:@"菜单栏-约会press"];
  64. [self setTabBarChildController:[[FindRootVC alloc] init] title:@"发现" image:@"菜单栏-发现" selectImage:@"菜单栏-发现press"];
  65. [self setTabBarChildController:[[PersonalRootVC alloc] init] title:@"个人" image:@"菜单栏-个人" selectImage:@"菜单栏-个人press"];
  66. }
  67. // 添加tabbar的子viewcontroller
  68. - (void)setTabBarChildController:(UIViewController*)controller title:(NSString*)title image:(NSString*)imageStr selectImage:(NSString*)selectImageStr{
  69. UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:controller];
  70. nav.tabBarItem.title = title;
  71. // if ([title isEqualToString:@"FM"]) {
  72. // nav.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -10);
  73. // }
  74. nav.tabBarItem.image = [[UIImage imageNamed:imageStr]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  75. nav.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageStr]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  76. [nav.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:NewGray2Color} forState:UIControlStateNormal];//NewRGBColor(74, 74, 74, 1.0)
  77. [nav.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:NewNavigationColor} forState:UIControlStateSelected];//NewRGBColor(255, 150, 38, 1.0)
  78. [self addChildViewController:nav];
  79. }
  80. - (void)changeRootViewControllerEvent:(NSInteger)changeIndex{
  81. [self setSelectedIndex:changeIndex];
  82. //__weak typeof(self) wself = self;
  83. //[wself setSelectedIndex:changeIndex];
  84. }
  85. - (void)dealloc
  86. {
  87. NSLog(@"Tabbar单例类销毁");
  88. }
  89. - (void)didReceiveMemoryWarning {
  90. [super didReceiveMemoryWarning];
  91. // Dispose of any resources that can be recreated.
  92. }
  93. /*
  94. #pragma mark - Navigation
  95. // In a storyboard-based application, you will often want to do a little preparation before navigation
  96. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  97. // Get the new view controller using [segue destinationViewController].
  98. // Pass the selected object to the new view controller.
  99. }
  100. */
  101. @end