RQMainTabBarViewController.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // RQMainTabBarViewController.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/21.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. // 主界面控制器
  8. #import "RQMainTabBarViewController.h"
  9. #import "RQBaseNavigationController.h"
  10. #import <Bugly/Bugly.h>
  11. @interface RQMainTabBarViewController ()
  12. @property (nonatomic, readonly, strong) RQMainTabBarViewModel *viewModel;
  13. @end
  14. @implementation RQMainTabBarViewController
  15. @dynamic viewModel;
  16. #pragma mark - System
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. // 初始化所有的子控制器
  20. [self rq_setupAllChildViewController];
  21. // set delegate
  22. self.tabBarController.delegate = self;
  23. }
  24. #pragma mark - Private Method
  25. - (void)rq_setupAllChildViewController {
  26. NSArray *titlesArray = @[@"驾考", @"我的"];
  27. NSArray *imageNamesArray = @[@"exam_normal",
  28. @"mine_normal",
  29. ];
  30. NSArray *selectedImageNamesArray = @[@"exam_select",
  31. @"mine_select",
  32. ];
  33. @weakify(self)
  34. /// 首页-考试
  35. UINavigationController *homePageNavigationController = ({
  36. @strongify(self)
  37. RQHomePageViewController *homePageViewController = [[RQHomePageViewController alloc] initWithViewModel:self.viewModel.homePageViewModel];
  38. RQTabBarItemTagType tagType = RQTabBarItemTagTypeHomePage;
  39. /// 配置
  40. [self rq_configViewController:homePageViewController imageName:imageNamesArray[tagType] selectedImageName:selectedImageNamesArray[tagType] title:titlesArray[tagType] itemTag:tagType];
  41. /// 添加到导航栏的栈底控制器
  42. [[RQBaseNavigationController alloc] initWithRootViewController:homePageViewController];
  43. });
  44. /// 我的
  45. UINavigationController *profileNavigationController = ({
  46. @strongify(self)
  47. RQProfileViewController *profileViewController = [[RQProfileViewController alloc] initWithViewModel:self.viewModel.profileViewModel];
  48. RQTabBarItemTagType tagType = RQTabBarItemTagTypeProfile;
  49. /// 配置
  50. [self rq_configViewController:profileViewController imageName:imageNamesArray[tagType] selectedImageName:selectedImageNamesArray[tagType] title:titlesArray[tagType] itemTag:tagType];
  51. [[RQBaseNavigationController alloc] initWithRootViewController:profileViewController];
  52. });
  53. /// 添加到tabBarController的子视图
  54. self.tabBarController.viewControllers = @[homePageNavigationController, profileNavigationController];
  55. /// 配置栈底
  56. [RQSharedAppDelegate.navigationControllerStack pushNavigationController:homePageNavigationController];
  57. }
  58. - (void)rq_configViewController:(UIViewController *)viewController imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName title:(NSString *)title itemTag:(RQTabBarItemTagType)tagType {
  59. UIImage *image = [UIImage imageNamed:imageName];
  60. image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  61. viewController.tabBarItem.image = image;
  62. viewController.tabBarItem.tag = tagType;
  63. UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
  64. selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  65. viewController.tabBarItem.selectedImage = selectedImage;
  66. viewController.tabBarItem.title = title;
  67. NSDictionary *normalAttr = @{NSForegroundColorAttributeName:RQ_MAIN_TEXT_COLOR_2,
  68. NSFontAttributeName:RQMediumFont(10)};
  69. NSDictionary *selectedAttr = @{NSForegroundColorAttributeName:RQ_MAIN_COLOR,
  70. NSFontAttributeName:RQMediumFont(10)};
  71. [viewController.tabBarItem setTitleTextAttributes:normalAttr forState:UIControlStateNormal];
  72. [viewController.tabBarItem setTitleTextAttributes:selectedAttr forState:UIControlStateSelected];
  73. [viewController.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, 0)];
  74. [viewController.tabBarItem setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
  75. }
  76. #pragma mark - UITabBarControllerDelegate
  77. - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
  78. if ([viewController.tabBarItem.title isEqualToString:@"我的"]) {
  79. return RQ_USER_MANAGER.isShouldLogin;
  80. // return YES;
  81. } else {
  82. return YES;
  83. }
  84. }
  85. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
  86. NSLog(@"viewController %@ %zd",viewController,viewController.tabBarItem.tag);
  87. [RQSharedAppDelegate.navigationControllerStack popNavigationController];
  88. [RQSharedAppDelegate.navigationControllerStack pushNavigationController:(UINavigationController *)viewController];
  89. }
  90. #pragma mark - 屏幕旋转
  91. - (BOOL)shouldAutorotate {
  92. return self.presentedViewController ? [self.presentedViewController shouldAutorotate] : ([self.tabBarController.selectedViewController qmui_hasOverrideUIKitMethod:_cmd] ? [self.tabBarController.selectedViewController shouldAutorotate] : YES);
  93. }
  94. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  95. // fix UIAlertController:supportedInterfaceOrientations was invoked recursively!
  96. // crash in iOS 9 and show log in iOS 10 and later
  97. // https://github.com/Tencent/QMUI_iOS/issues/502
  98. // https://github.com/Tencent/QMUI_iOS/issues/632
  99. UIViewController *visibleViewController = self.presentedViewController;
  100. if (!visibleViewController || visibleViewController.isBeingDismissed || [visibleViewController isKindOfClass:UIAlertController.class]) {
  101. visibleViewController = self.tabBarController.selectedViewController;
  102. }
  103. if ([visibleViewController isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) {
  104. return visibleViewController.supportedInterfaceOrientations;
  105. }
  106. return [visibleViewController qmui_hasOverrideUIKitMethod:_cmd] ? [visibleViewController supportedInterfaceOrientations] : SupportedOrientationMask;
  107. }
  108. @end