How to present a modal atop the current view in Swift(如何在 Swift 中的当前视图上呈现模态)
问题描述
(Xcode6、iOS8、Swift、iPad)
(Xcode6, iOS8, Swift, iPad)
我正在尝试创建一个经典的类似 Web 的模式视图,其中对话框的外部是灰色的".为此,我将模态视图的 backgroundColor 的 alpha 值设置为 0.5,如下所示:
I am trying to create a classic Web-like modal view, where the outside of the dialog box is "grayed-out." To accomplish this, I've set the alpha value of the backgroundColor of the view for the modal to 0.5, like so:
self.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5)
唯一的问题是当模态变为全屏时,呈现视图被移除.(参考 导航控制器上的透明模态视图).
The only problem is that when the modal becomes full-screen, the presenting view is removed. (Ref Transparent Modal View on Navigation Controller).
(对这里的概念有点恼火.为什么要删除底层视图?根据定义,模态显示在其他内容之上.一旦底层视图被删除,它就不再是真正的模态了.它介于模态之间和一个推式过渡.哇哇哇......无论如何......)
(A bit irritated at the concept here. Why remove the underlying view? A modal is, by definition, to appear atop other content. Once the underlying view is removed, it's not really a modal anymore. it's somewhere between a modal and a push transition. Wa wa wa... Anyway..)
为了防止这种情况发生,我在父控制器的 viewDidLoad
方法和 Storyboard 中将 modalPresentationStyle
设置为 CurrentContext
...但没有运气.
To prevent this from happening, I've set the modalPresentationStyle
to CurrentContext
in the viewDidLoad
method of the parent controller, and in Storyboard... but no luck.
self.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
self.navigationController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
当模态变为全屏时,如何防止正在显示的视图被移除?
How do I prevent the presenting view from being removed when the modal becomes full screen?
tyvm.. 更多信息如下.
tyvm.. more info below.
也在情节提要中,像这样(演示:当前上下文)
Also in Storyboard, like so (Presentation: Current Context)
感谢您的帮助...文档如下:
Thx for your help... documentation below:
推荐答案
首先,移除代码中所有显式的模态展示样式设置,然后执行以下操作:
First, remove all explicit setting of modal presentation style in code and do the following:
- 在故事板中,将 ModalViewController 的
modalPresentation
样式设置为Over Current context
- In the storyboard set the ModalViewController's
modalPresentation
style toOver Current context
- 选中 Root/Presenting ViewController -
Provide Context
和Define Context
中的复选框.他们似乎在不受限制地工作.
- Check the checkboxes in the Root/Presenting ViewController -
Provide Context
andDefine Context
. They seem to be working even unchecked.
这篇关于如何在 Swift 中的当前视图上呈现模态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Swift 中的当前视图上呈现模态


基础教程推荐
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01