我必须使用PresentModalViewController吗?

时间:2011-08-19 00:33:04

标签: ios core-animation

我正在做一些“有趣的”视图转换,我发现自己正在以一种感觉不对的方式解决“presentModalViewController”的功能。

我更喜欢完全控制模态视图控制器视图的显示,并完全跳过“presentModalViewController”。

但是,我不确定这样做的后果。

目前,我的代码看起来像这样(这只是一个伪代码示例,我不能使用内置转换,它们不会做我需要的):

    // 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, 
                                  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];

1 个答案:

答案 0 :(得分:1)

如果您不希望UIKit设置modalViewController并控制子视图控制器的显示和解除,则不要。您可以跳过presentModalViewController:animated:调用并手动添加或删除子视图,或者如果要切换到全新的视图控制器,则断开旧版本view与层次结构的连接并连接新的视图控制器等。其他展示方式包括UINavigationControllerUITabBarController,但不使用modalViewController方法。

更具体地说,您应该将应用程序UIWindow的{​​{3}}属性设置为新的视图控制器。

文档说:

  

根视图控制器提供窗口的内容视图。将视图控制器分配给此属性(以编程方式或使用Interface Builder)将视图控制器的视图安装为窗口的内容视图。如果窗口具有现有视图层次结构,则在安装新视图之前将删除旧视图。

请注意,文档提到了安装view作为层次结构内容视图的自动过程。我所说的是你可以使用提供的自动方法 - UIWindow用于根视图,modalViewController和其他系统用于非根视图 - 或者你可以手动完成,但它正在完成同样的事情。特别是因为rootViewController属性仅在iOS 4中存在,并且之前的应用程序在启动时使用自动生成的默认代码[window addSubview:rootView]

如果UIKit [UIWindow setRootViewController:]发生了一些额外的魔法,我完全准备好对此进行纠正。

相关问题