![](/img/trans.png)
[英]Dismiss current modal view controller and then present new modal view controller
[英]Present new view controller and invisibly dismiss the current chain of presented view controllers
所以我有一個視圖控制器鏈(通常是2 - 3),其中第一個是根視圖控制器,下一個是presentViewController:animated:
.
這是應用程序的入門部分,當用戶最終到達我需要顯示主UI的部分時,我想在顯示主UI之前關閉所有入門視圖控制器(根視圖控制器除外)視圖控制器,它是一個UITabBarController
。 我想要這樣做的原因是 - 我不再需要它們了,我不想將這些視圖控制器保留在內存中。
現在問題是 - 我希望這看起來像我只是呈現UITabBarController,我不希望用戶看到以前的viewcontrollers的解雇。
我試過的第一件事:
dismissViewControllerAnimated:completion:
在根視圖控制器上設置animated: NO
presentViewController:animated:completion:
with animated: YES
來顯示我的UITabBarController 可悲的是,在顯示呈現我的UITabBarController的動畫之前,閃過了根視圖控制器。
我試過的復雜解決方案:
dismissViewControllerAnimated:completion:
在根視圖控制器上設置animated: NO
presentViewController:animated:completion:
with animated: YES
來顯示我的UITabBarController presentViewController:
從根視圖控制器的視圖中刪除快照視圖 這是執行此操作的代碼。 它有點復雜,因為它還處理彈出根視圖控制器的子視圖控制器,因為根視圖控制器是UINavigationController:
- (void)popNavToRootAndPresentViewController:(UIViewController *)viewController
{
UINavigationController *rootVC = (UINavigationController *)self.view.window.rootViewController;
// if we have a chain of presented view controllers over our nav vc, then we need to dismiss them
if (rootVC.presentedViewController) {
// we need a snapshot view because even if dismissing without animation, root view controller will flash for a fraction of a second.
__block UIView *snapShotView = [self.view.window snapshotViewAfterScreenUpdates:NO];
[rootVC.view addSubview:snapShotView];
// hide the currently presented view controller chain without animation. This won't be visible since we've added the snapshot on the rootVC
[rootVC dismissViewControllerAnimated:NO completion:^{
// present the new view controller that we need
[rootVC presentViewController:viewController animated:YES completion:^{
// pop to root in case there are more than one pushed to nav stack
[rootVC popToRootViewControllerAnimated:NO];
// we don't need the snapshot view anymore
[snapShotView removeFromSuperview];
}];
}];
} else {
[rootVC presentViewController:viewController animated:YES completion:^{
// we presented our vc from the nav vc, so we can pop the nav vs itself to root - that won't be noticable
[rootVC popToRootViewControllerAnimated:NO];
}];
}
}
即使插入當前視圖層次結構的快照,我仍然有一個閃存。 使用此解決方案,dismissViewControllerAnimated導致視圖控制器的短暫閃爍,該視圖控制器是鏈中的前一個(呈現自身的那個)。
有沒有辦法可以在沒有閃爍的情況下實現預期的結果? 我想這樣的東西可以通過自定義父視圖控制器和子視圖控制器來實現,因為那時可以更好地控制視圖的替換方式,但我真的想保持簡單並使用presentViewController:
.
您可以簡單地在所有其他控制器上顯示(動畫)標簽欄控制器,一旦動畫完成,您可以將所有控制器關閉回根並再次顯示標簽欄控制器,這次不是動畫。
確保您保留對標簽欄控制器的引用,這樣您就不必重新分配新版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.