因為項目需要用類似脈脈的登錄樣式狡刘,所以記錄一下項目的主框架搭建端幼。脈脈使用的是NavigationController+TabBarController搭建募判,但是登錄的效果是push過去的合是,首先主框架是不變的厨诸,那么就是登錄push的效果了,一開始我想的是直接push到主框架搭建好的界面蜜托,但是導(dǎo)航欄會出現(xiàn)問題抄囚,所以放棄了。我采用的是修改TabBarController管理的第一個控制器橄务,如果沒有登錄就顯示登錄界面幔托,隱藏TabBar,如果是登錄了就直接顯示主頁。
首先自定義NavigationController和TabBarController,方便擴展,其次創(chuàng)建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: @"動態(tài)",
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 需判斷登錄狀態(tài)
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;
}
還需要在主界面里做設(shè)置重挑,就是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靜態(tài)單元格來展示,有退出按鈕的點擊
- (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];
}
}];
}