仿脈脈登錄效果

因?yàn)轫?xiàng)目需要用類似脈脈的登錄樣式,所以記錄一下項(xiàng)目的主框架搭建。脈脈使用的是NavigationController+TabBarController搭建,但是登錄的效果是push過(guò)去的,首先主框架是不變的,那么就是登錄push的效果了,一開(kāi)始我想的是直接push到主框架搭建好的界面,但是導(dǎo)航欄會(huì)出現(xiàn)問(wèn)題,所以放棄了。我采用的是修改TabBarController管理的第一個(gè)控制器,如果沒(méi)有登錄就顯示登錄界面,隱藏TabBar,如果是登錄了就直接顯示主頁(yè)。

首先自定義NavigationController和TabBarController,方便擴(kuò)展,其次創(chuàng)建4個(gè)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: @"動(dòng)態(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管理的第一個(gè)控制器:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if ([self isKindOfClass:[DynamicViewController class]]) {
        
        self.tabBarController.tabBar.hidden = NO;
    }else{
        self.tabBarController.tabBar.hidden = YES;
    }
}

在退出的時(shí)候也要做處理,我只是寫(xiě)的demo所以直接用的按鈕點(diǎn)擊。項(xiàng)目里是放在了我的界面里,使用tabbleView靜態(tài)單元格來(lái)展示,有退出按鈕的點(diǎn)擊

- (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];
            
        }
        
    }];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容