问题描述
项目新版本提测时经测试反馈发现iOS14系统在跳转多级页面后返回到根控制器时tabbar消失了,经过排查发现触发条件如下:
1.Xcode Version >=12.0.
2.ios Version>=14.0 && Version<14.2.
3.push层级大于2级.
4.执行[self.navigationController popToRootViewControllerAnimated:YE]跟[self.navigationController popToViewController:viewController animated:YES]且viewController为根控制器这两个方法的时候,animated为YES.
问题原因
假设A为根控制器,跳转流程为A->B->C
- (NSArray<UIViewController *> *)fd_popToRootViewControllerAnimated:(BOOL)animated {
//第一步
NSLog(@"%@", self.viewControllers);
NSArray *popedControllerArray = [selffd_popToRootViewControllerAnimated:animated];
//第二步
NSLog(@"viewControllers = %@", self.viewControllers);
returnpopedControllerArray;
}
打印发现上述情况下:
第一步
viewControllers = (
A,
B,
C
)
第二步
viewControllers = (
C,
A
)
正常情况下:
第一步
viewControllers = (
A,
B,
C
)
第二步
viewControllers = (
A
)
可以发现问题就出现在第二步,在有问题的情况下,viewControllers里的对象会多一个顶部控制器C,C的hidesBottomBarWhenPushed属性会覆盖根控制器A的hidesBottomBarWhenPushed属性,但是C的hidesBottomBarWhenPushed属性为YES,所以造成了tabbar不显示问题
解决方法
解决办法从出现问题的4个原因,其中一方面入手都能解决
1.添加分类,在hooc方法里把animated属性改为NO
- (NSArray<UIViewController *> *)fd_popToRootViewControllerAnimated:(BOOL)animated{
if ([[UIDevice currentDevice].systemVersion doubleValue] == 14.1 || [[UIDevice currentDevice].systemVersion doubleValue] == 14.0) {
animated =NO;
}
NSArray *popedControllerArray = [selffd_popToRootViewControllerAnimated:animated];
return popedControllerArray;
}
- (NSArray *)fd_popToViewController:(UIViewController*)viewControlleranimated:(BOOL)animated{
if ([[UIDevice currentDevice].systemVersion doubleValue] == 14.1 || [[UIDevice currentDevice].systemVersion doubleValue] == 14.0) {
animated =NO;
}
NSArray *popedControllerArray = [self fd_popToViewController:viewControlleranimated:animated];
return popedControllerArray;
}
2.添加分类,在hooc方法里把 self.topViewController的hidesBottomBarWhenPushed属性设置为NO
- (NSArray<UIViewController *> *)fd_popToRootViewControllerAnimated:(BOOL)animated{
if (self.viewControllers.count > 1) {
self.topViewController.hidesBottomBarWhenPushed = NO;
}
NSArray<UIViewController *> *popedControllerArray = [self fd_popToRootViewControllerAnimated:animated];
return popedControllerArray;
}
- (NSArray<UIViewController *> *)fd_popToViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.viewControllers.count > 1) {
self.topViewController.hidesBottomBarWhenPushed = NO;
}
NSArray<UIViewController *> *popedControllerArray = [self fd_popToViewController:viewController animated:animated];
return popedControllerArray;
}