gpt4 book ai didi

ios - 我必须使用 Present Modal ViewController 吗?

转载 作者:行者123 更新时间:2023-11-29 05:00:24 25 4
gpt4 key购买 nike

我正在做一些“有趣的” View 转换,并且我发现自己正在以一种感觉不正确的方式处理“presentModalViewController”的功能。

我更愿意完全控制模态视图 Controller View 的呈现,并完全跳过“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];

最佳答案

如果您不想让 UIKit 设置 modalViewController 并控制 subview Controller 的呈现和解除,那就不要这样做。您可以跳过 presentModalViewController:animated: 调用并手动添加或删除 subview ,或者如果您想切换到全新的 View Controller ,则断开旧 View Controller 的 view 与其他呈现方式包括 UINavigationController 或 UITabBarController ,它们不使用 modalViewController 方法。

更具体地说,您应该设置 rootViewController将应用程序的 UIWindow 属性添加到新的 View Controller 。

文档说:

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.

请注意,文档提到了将 View 安装为层次结构的内容 View 的自动过程。我的意思是,您可以使用提供的自动方法 - UIWindow 用于 Root View ,modalViewController 和其他系统用于非 Root View - 或者您也可以手动执行,但它正在完成同样的事情。特别是 rootViewController 属性自 iOS 4 起才存在,在此之前的应用程序在启动时使用自动生成的默认代码 [window addSubview:rootView]

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

关于ios - 我必须使用 Present Modal ViewController 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7115761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com