因为项目需要用类似脉脉的登录样式,所以记录一下项目的主框架搭建。脉脉使用的是NavigationController+TabBarController搭建,但是登录的效果是push过去的,首先主框架是不变的,那么就是登录push的效果了,一开始我想的是直接push到主框架搭建好的界面,但是导航栏会出现问题,所以放弃了。我采用的是修改TabBarController管理的第一个控制器,如果没有登录就显示登录界面,隐藏TabBar,如果是登录了就直接显示主页。
首先自定义NavigationController和TabBarController,方便扩展,其次创建4个tabBar管理的控制器,在tabBarController的操作如下:
#import "BaseTabBarController.h"
#import "BaseNavigationController.h"
#import "DynamicViewController.h"
#import "MessageViewController.h"
#import "MineViewController.h"
#import "ContactsViewController.h"
#import "LoginController.h"
#define kClassKey @"DynamicViewController"
#define kTitleKey @"MessageViewController"
#define kImgKey @"ContactsViewController"
#define kSelImgKey @"MineViewController"
@interface BaseTabBarController ()
@end
@implementation BaseTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *childItemsArray = @[@{kClassKey: @"DynamicViewController",
kTitleKey: @"动态",
kImgKey: @"shouye",
kSelImgKey: @""},
@{kClassKey: @"MessageViewController",
kTitleKey: @"消息",
kImgKey: @"xiaoxi",
kSelImgKey: @""},
@{kClassKey: @"ContactsViewController",
kTitleKey: @"人脉办事",
kImgKey: @"faxian",
kSelImgKey: @""},
@{kClassKey: @"MineViewController",
kTitleKey: @"我",
kImgKey: @"wode",
kSelImgKey: @""
}
];
[childItemsArray enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) {
UIViewController *vc = [NSClassFromString(dict[kClassKey]) new];
vc.title = dict[kTitleKey];
BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vc];
#warning 需判断登录状态
BOOL isLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"isLogin"];
if (isLogin == NO && idx == 0) {
nav = [[BaseNavigationController alloc] initWithRootViewController:[LoginController new]];
self.tabBar.hidden = YES;
}
UITabBarItem *item = nav.tabBarItem;
item.title = dict[kTitleKey];
item.image = [UIImage imageNamed:dict[kImgKey]];
item.selectedImage = [[UIImage imageNamed:dict[kSelImgKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[item setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor]} forState:UIControlStateSelected];
[self addChildViewController:nav];
}];
self.view.backgroundColor = [UIColor whiteColor];
self.tabBar.translucent = NO;
}
还需要在主界面里做设置,就是tabBar管理的第一个控制器:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([self isKindOfClass:[DynamicViewController class]]) {
self.tabBarController.tabBar.hidden = NO;
}else{
self.tabBarController.tabBar.hidden = YES;
}
}
在退出的时候也要做处理,我只是写的demo所以直接用的按钮点击。项目里是放在了我的界面里,使用tabbleView静态单元格来展示,有退出按钮的点击
- (void)clickLogoutBtn: (UIButton *)sender
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isLogin"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.tabBarController.childViewControllers enumerateObjectsUsingBlock:^(__kindof UINavigationController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (idx == 0) {
self.tabBarController.selectedIndex = 0;
[obj popToRootViewControllerAnimated:YES];
self.tabBarController.tabBar.hidden = YES;
[self.navigationController popToRootViewControllerAnimated:YES];
}
}];
}