Jak*_*lář 49 uiviewcontroller uikit modalviewcontroller ios ios13
iOS 13为模态呈现的视图控制器引入了modalPresentationStyle
.pageSheet
(及其兄弟姐妹.formSheet
)的新设计…
…我们可以通过向下滑动显示的视图控制器来消除这些页面(交互式消除)。尽管新的“拉动关闭”功能非常有用,但可能并不总是令人满意。
问题:我们如何关闭交互式解雇? -请记住,我们保持演示文稿样式不变。
Jak*_*lář 95
viewController.isModalInPresentation = true
Run Code Online (Sandbox Code Playgroud)
(禁用的交互式.pageSheet
解雇行为是这样的。)
UIViewController
包含一个名为的新属性isModalInPresentation
,必须将其设置true
为防止交互式解雇。.popover
。false
默认情况下,此属性。来自官方文档:如果为
true
,则UIKit会忽略视图控制器范围之外的事件,并防止在屏幕上交互关闭视图控制器。
func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
return false
}
Run Code Online (Sandbox Code Playgroud)
UIAdaptivePresentationControllerDelegate
包含一个名为的新方法presentationControllerShouldDismiss
。isModalInPresentation
属性设置为时,才调用此方法false
。提示:不要忘记分配presentationController的委托。
Bil*_*lal 33
如果您希望获得与以前的iOS版本(<iOS13)相同的行为(例如全屏模型演示),只需将目标视图控制器的演示样式设置为 UIModalPresentationStyle.fullScreen
let someViewController = \*VIEW CONTROLLER*\
someViewController.modalPresentationStyle = .fullScreen
Run Code Online (Sandbox Code Playgroud)
如果你使用的是故事板只需选择segua并选择Full Screen
形成Presentation
下拉。
如果您只想禁用交互式解雇并将新的演示文稿样式set UIViewController
属性设置isModalInPresentation
为true
。
if #available(iOS 13.0, *) {
someViewController.isModalInPresentation = true // available in IOS13
}
Run Code Online (Sandbox Code Playgroud)rae*_*aed 10
如果您有一些业务逻辑,例如在关闭之前应填写所有字段之类的内容,您应该:
如果ViewDidLoad
您的 ViewController 已出现在导航控制器中:
func viewDidLoad() {
self.navigationController?.presentationController?.delegate = self
}
Run Code Online (Sandbox Code Playgroud)
如果没有,只需使用
func viewDidLoad() {
self.presentationController?.delegate = self
}
Run Code Online (Sandbox Code Playgroud)
然后实现委托方法:
extension ViewController: UIAdaptivePresentationControllerDelegate {
func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
guard let text = firstName.text, text.isEmpty else { return false }
guard let text = lastName.text, text.isEmpty else { return false }
...
return true
}
}
Run Code Online (Sandbox Code Playgroud)
该物业isModalInPresentation
可能会有所帮助。
从文档:
当您将其设置为 时
true
,UIKit 会忽略视图控制器边界之外的事件,并防止视图控制器在屏幕上时交互式关闭。
你可以这样使用它:
let controller = MyViewController()
controller.isModalInPresentation = true
self.present(controller, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您使用故事板来布局您的 UI,我发现在使用导航控制器时禁用此交互式关闭的最佳方法是将属性检查器中导航控制器的显示从自动更改为全屏。导航堆栈中的所有视图控制器都将全屏显示并且无法被用户关闭。
归档时间: |
|
查看次数: |
12901 次 |
最近记录: |