ios 呈现多个模态视图控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19724166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Present multiple modal view controllers?
提问by trickster77777
update:
I've faced this problem again, and found another way. If presenting controller is not embedded in navigation controller, it will be hidden if presented controller is not fullscreen and will became black. Method setModalPresentationStyle:UIModalPresentationCurrentContext can be applied only to navigation controller. So embed presenting controller in UINavigationController, set UIModalPresentationCurrentContext to it and present new controller - you will get dialog controller.
更新:
我再次遇到这个问题,并找到了另一种方法。如果呈现控制器没有嵌入到导航控制器中,如果呈现控制器不是全屏显示,它将被隐藏并变为黑色。方法 setModalPresentationStyle:UIModalPresentationCurrentContext 只能应用于导航控制器。因此,在 UINavigationController 中嵌入呈现控制器,将 UIModalPresentationCurrentContext 设置为它并呈现新控制器 - 您将获得对话框控制器。
I'm presenting search controller, it have tableView that push on stack detailed controller.
我正在展示搜索控制器,它具有推送堆栈详细控制器的 tableView。
Detailed controller can present view controller with message, it consists from small UIView and semitransparent background.
详细的控制器可以用消息来呈现视图控制器,它由小的 UIView 和半透明的背景组成。
Problem: when last view controller presented, all view controllers under it becomes hidden and controller, that presented search controller becomes visible.
问题:当最后一个视图控制器呈现时,它下面的所有视图控制器都变成隐藏的,并且控制器,呈现的搜索控制器变得可见。
Here what I'm doing:
这是我在做什么:
SearchViewController *viewController = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil];
viewController.data = dataArray;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.navigationController setModalPresentationStyle:UIModalPresentationCurrentContext];
[self.navigationController presentViewController:navigationController animated:YES completion:nil];
than table pushes detailed view:
比表推详细视图:
DetailViewController *viewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
[viewController setHidesBottomBarWhenPushed:YES];
viewController.dataItem = [data objectAtIndex:(NSUInteger) [indexPath row]];
[self.navigationController pushViewController:viewController animated:YES];
and detailed view presenting message box:
和显示消息框的详细视图:
MessageController *controller = [[MessageController alloc] initWithNibName:@"MessageController" bundle:nil];
controller.message = message;
[self presentViewController:controller animated:YES completion:nil];
When it's dismissed, all controllers under it became visible.
当它被关闭时,它下的所有控制器都变得可见。
update:
更新:
all I wanted is to present modally a view controller that will have uitableview. From this table to show detailed view that will be able to show message box. Message box must be another view controller. And when message box is shown all two preceding controllers disappears. that is the issue.
我想要的只是以模态方式呈现一个具有 uitableview 的视图控制器。从这个表中显示详细视图,将能够显示消息框。消息框必须是另一个视图控制器。当消息框显示时,所有两个前面的控制器都消失了。这就是问题所在。
采纳答案by Gtx
The trivial way to attempt to achieve that is to just create the VCs you want to present modally and present one after the other. Unfortunately, this is not gonna work. What you get is just the first VC presented, all others just go nowhere. UIKit just won't cooperate with you here.
尝试实现这一目标的简单方法是创建您想要以模态呈现的 VC,然后一个接一个地呈现。不幸的是,这行不通。你得到的只是第一个 VC 展示,其他所有的都无处可去。UIKit 不会在这里与您合作。
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
回答by SlimeBaron
In some situations, you could have each modally presented view controller present the next one.
在某些情况下,您可以让每个以模态呈现的视图控制器呈现下一个。
let window = UIApplication.sharedApplication().keyWindow!
if let modalVC = window.rootViewController?.presentedViewController {
modalVC.presentViewController(vc, animated: true, completion: nil)
} else {
window.rootViewController!.presentViewController(vc, animated: true, completion: nil)
}
回答by Rei Kubonaga
Dismiss all view controllers above the presented view controller.
关闭呈现的视图控制器上方的所有视图控制器。
Apple's documentation follows:
苹果的文档如下:
If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.
如果您连续呈现多个视图控制器,从而构建呈现的视图控制器堆栈,则在堆栈中较低的视图控制器上调用此方法会解除其直接子视图控制器以及堆栈中该子视图之上的所有视图控制器。发生这种情况时,只有最顶层的视图以动画方式消失;任何中间视图控制器都被简单地从堆栈中删除。最顶层的视图使用其模态转换样式被关闭,这可能与堆栈中其他视图控制器使用的样式不同。
Dismiss two modal view controllers.
关闭两个模态视图控制器。
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
回答by Robert Chen
This answer might seem a little unconventional, but it works. It lets you "open multiple modals", but only show one at a time. The key is to have just one modal, but use a Tab Bar Controllerto switch between View Controllers.
这个答案可能看起来有点不合常规,但它确实有效。它允许您“打开多个模态”,但一次只显示一个。关键是只有一个模态,但使用Tab Bar Controller在View Controller之间切换。
- Wire your modal segue from the blue (presenting) View Controllerto a modal container (which is just a regular View Controller)
- The modal container holds just a Container View, whose sole purpose is to embed a Tab Bar Controller.
- Embed a Tab Bar Controllerinside the Container View
- Each tab can be a separate View Controller, representing "multiple modals"
- Set the Tab Bar Controller's Tab Barto hidden
- Switch between modals using
tabBarController?.selectedIndex = 1
- Close the modal normally with
dismissViewControllerAnimated(true, completion: nil)
- 将您的模态转场从蓝色(呈现)视图控制器连接到模态容器(这只是一个常规的视图控制器)
- 模态容器仅包含一个Container View,其唯一目的是嵌入Tab Bar Controller。
- 在容器视图中嵌入标签栏控制器
- 每个选项卡可以是一个单独的View Controller,代表“多个模式”
- 将Tab Bar Controller的Tab Bar 设置为hidden
- 使用模式在模式之间切换
tabBarController?.selectedIndex = 1
- 正常关闭模态
dismissViewControllerAnimated(true, completion: nil)
It would look something like this:
它看起来像这样:
回答by rizzes
Set the correct modalPresentationStyle
on boththe navigationController
AND the viewController
itself.
设置正确modalPresentationStyle
上都在navigationController
和viewController
自己。
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
vcToPresent.modalPresentationStyle = UIModalPresentationFormSheet;
vcToPresent.preferredContentSize = CGSizeMake(650, 450);
[self presentViewController:vcToPresent animated:YES completion:^{
self.navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
}];`
回答by Antzi
I found a solution: provide your own custom animation:
我找到了一个解决方案:提供您自己的自定义动画:
let transition: CATransition = CATransition()
transition.duration = 0.5
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionReveal
transition.subtype = kCATransitionFromBottom
self.view.window!.layer.add(transition, forKey: nil)
self.presentingViewController?.presentingViewController?.dismiss(animated: false, completion: nil)