While investigating a memory leak I discovered a problem related to the technique of calling setRootViewController:
inside a transition animation block:
[UIView transitionWithView:self.window
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{ self.window.rootViewController = newController; }
completion:nil];
If the old view controller (the one being replaced) is currently presenting another view controller, then the above code does not remove the presented view from the view hierarchy.
That is, this sequence of operations...
- X becomes Root View Controller
- X presents Y, so that Y's view is on screen
- Using
transitionWithView:
to make Z the new Root View Controller
...looks OK to the user, but the Debug View Hierarchy tool will reveal that Y's view is still there behind Z's view, inside a UITransitionView
. That is, after the three steps above, the view hierarchy is:
- UIWindow
- UITransitionView
- UIView (Y's view)
- UIView (Z's view)
- UITransitionView
I suspect this is a problem because, at the time of the transition, X's view isn't actually part of the view hierarchy.
If I send dismissViewControllerAnimated:NO
to X immediately before transitionWithView:
, the resulting view hierarchy is:
- UIWindow
- UIView (X's view)
- UIView (Z's view)
If I send dismissViewControllerAnimated:
(YES or NO) to X, then perform the transition in the completion:
block, then the view hierarchy is correct. Unfortunately, that interferes with the animation. If animating the dismissal, it wastes time; if not animating, it looks broken.
I'm trying some other approaches (e.g., making a new container view controller class to serve as my root view controller) but haven't found anything that works. I'll update this question as I go.
The ultimate goal is to transition from the presented view to a new root view controller directly, and without leaving stray view hierarchies around.
I try a simple thing which work for me on iOs 9.3 : just remove the old viewController's view from its hierarchy during
dismissViewControllerAnimated
completion.Let's work on X, Y, and Z view as explained by benzado :
Which give :
In my case, X and Y are well dealloc and their's view are no more in hierarchy !
I faced this issue and it annoyed me for a whole day. I've tried @Rich's obj-c solution and it turns out when I want to present another viewController after that, I will be blocked with a blank UITransitionView.
Finally, I figured out this way and it worked for me.
Alright, now all you have to do is call
[self setRootViewController:newViewController];
when you want to switch root view controller.I came to this issue when using this code:
Disabling this code, fixed the problem. I managed to get this working by only enabling this transition animation when the filterbar which gets animated is initialised.
It's not really the answer you're looking for, but it could bring you on the right pad for finding your solution.
I had a similar issue recently. I had to manually remove that
UITransitionView
from the window to fix the problem, then call dismiss on the previous root view controller to ensure its deallocated.The fix is not really very nice but unless you've found a better way since posting the question, its the only thing I've found to work!
viewController
is just thenewController
from your original question.I hope this helps you fix your problem too, it's an absolute pain in the arse!
(See edit history for other Swift versions)
For a nicer implementation as a extension on
UIWindow
allowing an optional transition to be passed in.Usage:
Or