I'm doing some "interesting" view transitions, and I'm finding myself working around the functionality of "presentModalViewController" in a way that doesn't feel right.
I'd prefer to take total control over the presentation of the modal view controller's view and skip "presentModalViewController" altogether.
However, I'm not sure about the ramifications of doing this.
Currently, I've got code that looks works something like like this (this is just a pseudo-code example, and I can't use the built in transitions, they won't do what I need):
// Create the child view controller:
ModalViewController * child = [[ModalViewController alloc] init];
// Present it:
[parentViewController presentModalViewController:child animated:NO];
// This rect is what the child view's ultimate "destination" should be,
// and, what the parent view's old frame was:
CGRect frame = child.view.frame;
// Put the parent view controller's view back in the window:
[child.view.window insertSubview:parentViewController.view belowSubview:child.view];
// Show it if it's hidden:
[parentViewController.view setHidden:NO];
// Put the parent back where it was:
[parentViewController.view setFrame:frame];
// Put the child at the "top" of the screen (the bottom edge
// of the child's view is at the top of the screen):
[child.view setFrame:CGRectMake(frame.origin.x,
frame.origin.y - frame.size.height,
开发者_StackOverflow社区 frame.size.width,
frame.size.height)];
// Animate a transition which slide the parent and child views
// down together:
[UIView animateWithDuration:0.7 animations:^(void) {
child.view.frame = frame;
parentViewController.view.frame = CGRectMake(frame.origin.x,
frame.origin.y + frame.size.height,
frame.size.width,
frame.size.height);
} completion:^(BOOL finished) {
// We're done, remove the parent view from the window
// like it's supposed to be:
[parentViewController.view removeFromSuperview];
}];
[child release];
If you don't want to have UIKit
set modalViewController
and control the presentation and dismissal of the child view controller, then don't. You can skip the presentModalViewController:animated:
call and manually add or remove subviews, or if you want to switch to an entirely new view controller then disconnect the old one's view
from the heirarchy and connect the new one, etc. Other ways of presenting include UINavigationController
or a UITabBarController
, and they don't use the modalViewController
methods.
To be more specific, you should set the rootViewController
property of your application's UIWindow
to the new view controller.
Docs say:
The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.
Note that the docs mention an automatic process of installing the view
as the content view of the heirarchy. What I'm saying is you can use the provided automatic methods - UIWindow
for root views, modalViewController
and other systems for non-root views - or you can do it manually, but it's accomplishing the same thing. Particularly since the rootViewController
property has only existed since iOS 4, and applications prior to this used auto-generated default code of [window addSubview:rootView]
at launch.
If UIKit
has some extra magic occurring in [UIWindow setRootViewController:]
I'm totally prepared to be corrected on this though.
精彩评论