版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2017.10.21 |
前言
目前市面上的APP架構(gòu)基本都是一樣的橄抹,都是
UITabBarController
作為根控制器,然后添加UINavigationController
作為子控制器美尸,然后再添加幾個(gè)主頁(yè)控制器鞠柄,作為導(dǎo)航的根控制器。這里有一個(gè)問(wèn)題就是子控制器的生命周期問(wèn)題械馆。
問(wèn)題引出
關(guān)于注意UITabBarController
子控制器生命周期的問(wèn)題胖眷,還是在iOS11以后,在iOS11正式版出來(lái)以后霹崎,我就發(fā)現(xiàn)一個(gè)問(wèn)題珊搀,凡是從幾個(gè)主頁(yè)面到二級(jí)頁(yè)面的控制器,UITabBar
都會(huì)先顯示然后隱藏尾菇,做大約0.5s的一個(gè)動(dòng)畫(huà)境析,于是囚枪,我就要修改,想到第一個(gè)方案就是生命周期方法中強(qiáng)制隱藏和顯示UITarBar簿晓。
這里我就在項(xiàng)目中- (void)viewWillAppear:(BOOL)animated
中強(qiáng)制顯示和在- (void)viewWillDisappear:(BOOL)animated
中進(jìn)行強(qiáng)制隱藏眶拉,但是發(fā)現(xiàn)還是不多,通過(guò)打斷點(diǎn)憔儿,我發(fā)現(xiàn)原來(lái)是生命周期的問(wèn)題忆植。
素材準(zhǔn)備
下面我們就看一下準(zhǔn)備的實(shí)現(xiàn)素材。下面直接看代碼谒臼。
1. AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
JJTabBarVC *tabBarVC = [[JJTabBarVC alloc] init];
self.window.rootViewController = tabBarVC;
[self.window makeKeyAndVisible];
return YES;
}
2. JJTabBarVC.m
#import "JJTabBarVC.h"
#import "ZBLivelistController.h"
#import "ZBVideoAlbumVC.h"
#import "ZBShortVideoVC.h"
@interface JJTabBarVC ()
@end
@implementation JJTabBarVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
[self addChildViewController];
}
#pragma mark - Object Private Function
- (void) addChildViewController
{
[self addChirdViewControllerWithClass:[[ZBLivelistController alloc] init] andTitle:@"直播" andImage:[UIImage imageNamed:@"TabBar_LiveTab_normal"] selectImage:[UIImage imageNamed:@"TabBar_LiveTab_normal"]];
[self addChirdViewControllerWithClass:[[ZBVideoAlbumVC alloc] init] andTitle:@"首頁(yè)" andImage:[UIImage imageNamed:@"TabBar_HomeTab_normal"] selectImage:[UIImage imageNamed:@"TabBar_HomeTab_normal"]];
[self addChirdViewControllerWithClass:[[ZBShortVideoVC alloc] init] andTitle:@"短視頻" andImage:[UIImage imageNamed:@"TabBar_shortVideoTab_normal"] selectImage:[UIImage imageNamed:@"TabBar_shortVideoTab_normal"]];
}
- (void) addChirdViewControllerWithClass:(UIViewController *)vc andTitle:(NSString *)title andImage:(UIImage *)image selectImage:(UIImage *)selectImage
{
vc.title = title;
vc.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc.tabBarItem.selectedImage = [selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self addChildViewController:nav];
}
@end
3. ZBLivelistController.m
#import "ZBLivelistController.h"
@interface ZBLivelistController ()
@end
@implementation ZBLivelistController
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tabBarController.tabBar.hidden = NO;
NSLog(@"ZBLivelistController --- viewWillAppear");
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.tabBarController.tabBar.hidden = YES;
NSLog(@"ZBLivelistController --- viewWillDisappear");
}
@end
4. ZBVideoAlbumVC.m
#import "ZBVideoAlbumVC.h"
@interface ZBVideoAlbumVC ()
@end
@implementation ZBVideoAlbumVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tabBarController.tabBar.hidden = NO;
NSLog(@"ZBVideoAlbumVC --- viewWillAppear");
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.tabBarController.tabBar.hidden = YES;
NSLog(@"ZBVideoAlbumVC --- viewWillDisappear");
}
@end
5. ZBShortVideoVC.m
#import "ZBShortVideoVC.h"
@interface ZBShortVideoVC ()
@end
@implementation ZBShortVideoVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tabBarController.tabBar.hidden = NO;
NSLog(@"ZBShortVideoVC --- viewWillAppear");
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.tabBarController.tabBar.hidden = YES;
NSLog(@"ZBShortVideoVC --- viewWillDisappear");
}
@end
運(yùn)行起來(lái)如下圖所示朝刊。
但是測(cè)試一下,我們就會(huì)發(fā)現(xiàn)蜈缤,當(dāng)點(diǎn)擊短視頻拾氓,再點(diǎn)擊直播頁(yè)面的時(shí)候,tabBar沒(méi)有了底哥,所以我們看打印輸出咙鞍。
2017-10-22 00:22:26.648393+0800 testDemo[6569:592943] ZBLivelistController --- viewWillAppear
2017-10-22 00:22:28.486030+0800 testDemo[6569:592943] ZBLivelistController --- viewWillDisappear
2017-10-22 00:22:28.504998+0800 testDemo[6569:592943] ZBShortVideoVC --- viewWillAppear
2017-10-22 00:22:30.118026+0800 testDemo[6569:592943] ZBLivelistController --- viewWillAppear
2017-10-22 00:22:30.119026+0800 testDemo[6569:592943] ZBShortVideoVC --- viewWillDisappear
這里就會(huì)發(fā)現(xiàn),當(dāng)我們點(diǎn)擊短視頻趾徽,在點(diǎn)擊直播续滋,就會(huì)發(fā)現(xiàn),程序會(huì)先調(diào)用直播頁(yè)面ZBLivelistController --- viewWillAppear
孵奶,而后調(diào)用短視頻頁(yè)面的ZBShortVideoVC --- viewWillDisappear
疲酌,這就導(dǎo)致了UITabBar被隱藏掉了,為什么會(huì)這樣呢了袁,和我們平時(shí)看到的控制器是不一樣的朗恳,我們平時(shí)看到的都是先調(diào)用要消失控制器的willDisappear,后調(diào)用要出現(xiàn)的控制器視圖的willAppear方法载绿。在這里就是完全反的粥诫。這里我們?cè)趺唇鉀Q這個(gè)問(wèn)題呢?
問(wèn)題解決
有了上面的問(wèn)題崭庸,我們就要解決臀脏,最簡(jiǎn)單直接的方法就是在三個(gè)子控制器中多加一個(gè)方法,如下所示冀自。
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.tabBarController.tabBar.hidden = NO;
}
這樣就解決了生命周期的問(wèn)題,解決隱藏時(shí)出現(xiàn)的錯(cuò)誤秒啦。這樣就可以在視圖出現(xiàn)的時(shí)候就不要隱藏UITabBar熬粗,解決了這個(gè)問(wèn)題。
后記
未完余境,待續(xù)~~~