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];
    }
}

以上,希望有帮助到大家

相关推荐
茶底世界之下12 小时前
洗衣房的午后斜光:用 Awaking 把光和影分开
ios·app·产品
我是谁的程序员1 天前
iOS 性能分析在 Windows 上怎么做,用 KXBuild 的 kxdevice 替代 Instruments
后端·ios
开心就好20251 天前
通配符 Bundle ID 和精确 Bundle ID 怎么选,推送、iCloud 能不能用通配符
后端·ios
布莱克6051 天前
Swift 枚举详解:从基础语法到关联值与模式匹配
开发语言·ios·swift·枚举
茶底世界之下1 天前
冬日窗边的早餐:用 Awaking 留住冷光里的暖意
ios·app·产品
布莱克6052 天前
Swift 闭包详解:从语法到实战的完整指南
服务器·开发语言·ios·ssh·swift·闭包
茶底世界之下3 天前
雨后的黄电车:用 Awaking 给灰色街景定一个主角
ios·app·产品
恋猫de小郭3 天前
iOS for Web ,把原生 iOS App 跑到进浏览器里面,倒反天罡
android·前端·ios
00后程序员3 天前
iOS 实时活动怎么开发,Windows 上用 KXBuild 和 ActivityKit 写灵动岛
后端·ios
iReaShare3 天前
如何在没有 Wi-Fi 的情况下备份 iPhone
ios