跨Navigation跳转(类似微信)方案一

本文介绍了一种在微信应用中优化聊天界面跳转和返回体验的方法。通过自定义StartNavigationController并调整tabbarController层次结构,使得从聊天界面返回时能正确回到会话列表,解决了原有实现中返回显示不正确的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是方案一,不推荐使用,在我们的项目(项目比结构较复杂)时候一段时间后发现了一个bug,有时会在tabbar上留一行白色区域,正好是navgationBar的高度.而且这种做法比较复杂.不过是一种很有意思的思路!

效果

(2017-12-15更新:微信已经不再采用下面的跳转方式了)

微信跳转,在通讯录Navi点人聊天

聊天界面返回的时候tabbar回到了会话列表“微信”的Navi上,而且左滑返回看到的也是“微信”的Navi

##实现思路

一个OrignNavigationController包裹我们正常的UITabBarController

以往的写法

MainTabBarController *mainTabBarController = [[MainTabBarController alloc] init];
self.window.rootViewController = mainTabBarController;
复制代码

我们的写法

MainTabBarController *mainTabBarController = [[MainTabBarController alloc] init];
StartNavigationController *nav = [[StartNavigationController alloc] initWithRootViewController:mainTabBarController];
self.window.rootViewController = nav;
复制代码

点人聊天时我们的操作 1.用tabbarController的NavigationController(即StartNavigationController)进行push操作跳转到聊天界面

UINavigationController* nav = ((AppDelegate*)[UIApplication sharedApplication].delegate).mainController.navigationController; // 我们用appdele持有tabbarController
[nav pushViewController:sessionVC animated:YES];
复制代码

2.上面操作以后的效果看起来确实有跳转,但是如果此时点击聊天界面的返回按钮(以微信为例)返回到的界面是"通讯里"而不是"微信"的会话列表 3.偷梁换柱-所以我们需要做点操作让返回的时候回到"微信" 在聊天界面中作如下操作

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    MainTabBarController* mainTabBarController = ((AppDelegate*)[UIApplication sharedApplication].delegate).mainController;
    [mainTabBarController setSelectedIndex:MessageIndex]; // tabbar切换回"微信"Navi
    // 下一步很关键也很难理解,所以重点解释下
    /*
    	1.要明白self.navigationController是StartNavigationController
    	2.此时整个app的结构是window包着StartNavigationController,StartNavigationController包着一个栈底控制器MainTabbBarController和一个栈顶控制器聊天Controller
    */
    [self.navigationController setViewControllers:@[mainTabBarController,self]];
    UINavigationController* navigation = mainTabBarController.viewControllers[self.comingFromNavigationController - 1];
    [navigation popToRootViewControllerAnimated:NO]; // 最后记得把"通讯录"Navi要popToRoot
}
复制代码

上面的操作主要是对tabbarController的层次界面情况进行调整

4.最后要注意的 StartNavigationController要自定义写一个,并且设置

[self.navigationBar setTranslucent:YES];
复制代码

不然会导致该Navi下的控制器下移44的高度

转载于:https://juejin.im/post/5a31067af265da431f4b11ea

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值