// // RQMainTabBarViewController.m // RQCommon // // Created by 张嵘 on 2018/11/21. // Copyright © 2018 张嵘. All rights reserved. // 主界面控制器 #import "RQMainTabBarViewController.h" #import "RQBaseNavigationController.h" #import @interface RQMainTabBarViewController () @property (nonatomic, readonly, strong) RQMainTabBarViewModel *viewModel; @end @implementation RQMainTabBarViewController @dynamic viewModel; #pragma mark - System - (void)viewDidLoad { [super viewDidLoad]; // 初始化所有的子控制器 [self rq_setupAllChildViewController]; // set delegate self.tabBarController.delegate = self; } #pragma mark - Private Method - (void)rq_setupAllChildViewController { NSArray *titlesArray = @[@"驾考", @"我的"]; NSArray *imageNamesArray = @[@"exam_normal", @"mine_normal", ]; NSArray *selectedImageNamesArray = @[@"exam_select", @"mine_select", ]; @weakify(self) /// 首页-考试 UINavigationController *homePageNavigationController = ({ @strongify(self) RQHomePageViewController *homePageViewController = [[RQHomePageViewController alloc] initWithViewModel:self.viewModel.homePageViewModel]; RQTabBarItemTagType tagType = RQTabBarItemTagTypeHomePage; /// 配置 [self rq_configViewController:homePageViewController imageName:imageNamesArray[tagType] selectedImageName:selectedImageNamesArray[tagType] title:titlesArray[tagType] itemTag:tagType]; /// 添加到导航栏的栈底控制器 [[RQBaseNavigationController alloc] initWithRootViewController:homePageViewController]; }); /// 我的 UINavigationController *profileNavigationController = ({ @strongify(self) RQProfileViewController *profileViewController = [[RQProfileViewController alloc] initWithViewModel:self.viewModel.profileViewModel]; RQTabBarItemTagType tagType = RQTabBarItemTagTypeProfile; /// 配置 [self rq_configViewController:profileViewController imageName:imageNamesArray[tagType] selectedImageName:selectedImageNamesArray[tagType] title:titlesArray[tagType] itemTag:tagType]; [[RQBaseNavigationController alloc] initWithRootViewController:profileViewController]; }); /// 添加到tabBarController的子视图 self.tabBarController.viewControllers = @[homePageNavigationController, profileNavigationController]; /// 配置栈底 [RQSharedAppDelegate.navigationControllerStack pushNavigationController:homePageNavigationController]; } - (void)rq_configViewController:(UIViewController *)viewController imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName title:(NSString *)title itemTag:(RQTabBarItemTagType)tagType { UIImage *image = [UIImage imageNamed:imageName]; image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; viewController.tabBarItem.image = image; viewController.tabBarItem.tag = tagType; UIImage *selectedImage = [UIImage imageNamed:selectedImageName]; selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; viewController.tabBarItem.selectedImage = selectedImage; viewController.tabBarItem.title = title; NSDictionary *normalAttr = @{NSForegroundColorAttributeName:RQ_MAIN_TEXT_COLOR_2, NSFontAttributeName:RQMediumFont(10)}; NSDictionary *selectedAttr = @{NSForegroundColorAttributeName:RQ_MAIN_COLOR, NSFontAttributeName:RQMediumFont(10)}; [viewController.tabBarItem setTitleTextAttributes:normalAttr forState:UIControlStateNormal]; [viewController.tabBarItem setTitleTextAttributes:selectedAttr forState:UIControlStateSelected]; [viewController.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, 0)]; [viewController.tabBarItem setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; } #pragma mark - UITabBarControllerDelegate - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { if ([viewController.tabBarItem.title isEqualToString:@"我的"]) { return RQ_USER_MANAGER.isShouldLogin; // return YES; } else { return YES; } } - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { NSLog(@"viewController %@ %zd",viewController,viewController.tabBarItem.tag); [RQSharedAppDelegate.navigationControllerStack popNavigationController]; [RQSharedAppDelegate.navigationControllerStack pushNavigationController:(UINavigationController *)viewController]; } #pragma mark - 屏幕旋转 - (BOOL)shouldAutorotate { return self.presentedViewController ? [self.presentedViewController shouldAutorotate] : ([self.tabBarController.selectedViewController qmui_hasOverrideUIKitMethod:_cmd] ? [self.tabBarController.selectedViewController shouldAutorotate] : YES); } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { // fix UIAlertController:supportedInterfaceOrientations was invoked recursively! // crash in iOS 9 and show log in iOS 10 and later // https://github.com/Tencent/QMUI_iOS/issues/502 // https://github.com/Tencent/QMUI_iOS/issues/632 UIViewController *visibleViewController = self.presentedViewController; if (!visibleViewController || visibleViewController.isBeingDismissed || [visibleViewController isKindOfClass:UIAlertController.class]) { visibleViewController = self.tabBarController.selectedViewController; } if ([visibleViewController isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) { return visibleViewController.supportedInterfaceOrientations; } return [visibleViewController qmui_hasOverrideUIKitMethod:_cmd] ? [visibleViewController supportedInterfaceOrientations] : SupportedOrientationMask; } @end