iOS 26手势返回到根页面时TabBar的动效问题

问题描述

我在适配完iOS 26时发现一个很奇怪的问题:

在第一次手势返回根页面时,tabBar没有渐显动画直接显示在顶部。但是如果第一次没有返回,再次手势返回时则有渐显动效。如下图所示:(测试设备iPhone 17;iOS 26.2)

图片

代码实现

一开始代码实现如下,在跳页的时候使用hidesBottomBarWhenPushed来隐藏tabBar

ini 复制代码
SecondVC *secondVC = [[SecondVC alloc] init];
secondVC.titleName = @"首页";
secondVC.hidesBottomBarWhenPushed = YES; // 隐藏tabbar
UIViewController *currentVC = self.window.rootViewController;
if ([currentVC isKindOfClass:[UITabBarController class]]) {
   UITabBarController *tabBarController = (UITabBarController *)currentVC;
   UINavigationController *homeNav = (UINavigationController *)tabBarController.viewControllers[0];
   [homeNav pushViewController:secondVC animated:YES];
}

后面经过测试,发现使用[tabBarController setTabBarHidden:NO animated:animated]; 就正常渐显了。

关键代码如下

  1. 创建首页的UINavigationController时设置代理
ini 复制代码
RootTabBarController *tabBarController = [[RootTabBarController alloc] init];
    self.tabController = tabBarController;
    
    // 设置首页tab
    UINavigationController *homeNav = [[UINavigationController alloc] initWithRootViewController:homeVC];
    homeNav.tabBarItem.title = @"首页";
    homeNav.tabBarItem.image = [UIImage systemImageNamed:@"house"];
    // 设置代理
    homeNav.delegate = tabBarController;
    
    // 设置我的tab
    UINavigationController *profileNav = [[UINavigationController alloc] initWithRootViewController:profileVC];
    profileNav.tabBarItem.title = @"我的";
    profileNav.tabBarItem.image = [UIImage systemImageNamed:@"person"];
    // 设置代理
    profileNav.delegate = tabBarController;
    
    [tabBarController setViewControllers:@[homeNav, profileNav]];
    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];
  1. 实现 UINavigationControllerDelegate
objectivec 复制代码
#pragma mark - UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断是否为根视图控制器
    if ([navigationController.viewControllers indexOfObject:viewController] == 0) {
        // 返回到根页面,显示tabbar
        [self setTabBarHidden:NO animated:animated];
    } else {
        // 跳转到子页面,隐藏tabbar
        [self setTabBarHidden:YES animated:animated];
    }
}

以上,希望有帮助到大家

相关推荐
美狐美颜SDK开放平台7 小时前
多场景美颜SDK解决方案:直播APP(iOS/安卓)开发接入详解
android·人工智能·ios·音视频·美颜sdk·第三方美颜sdk·短视频美颜sdk
wuxianda10308 小时前
苹果App上架4.3a被拒解决方案汇报总结
ios·uni-app·objective-c·cocoa·苹果上架·4.3a
SameX13 小时前
用 SpriteKit 做了个存钱罐 App,30 枚硬币同时掉帧率直接崩了
ios
for_ever_love__13 小时前
UI学习:单例传值
学习·ui·ios·objective-c
for_ever_love__14 小时前
UI学习:通知传值
学习·ui·ios·objective-c
2501_9151063214 小时前
在Mac上搭建iOS开发环境的详细步骤与注意事项
ide·vscode·macos·ios·个人开发·swift·敏捷流程
想个名字想老半天14 小时前
uni 离线打包 ios,适用于自定义 ios系统最低适配 保姆级教程
macos·ios·cocoa
No Silver Bullet14 小时前
iOS开发进阶(二十四):一文读懂iOS发布证书,描述文件到期后,在工程中如何进行替换
ios
库奇噜啦呼15 小时前
【iOS】源码学习-类与对象底层原理
学习·ios·cocoa