美文网首页
iOS13中present画面消除浮动效果,禁用与监测下拉手势

iOS13中present画面消除浮动效果,禁用与监测下拉手势

作者: 七夕猪 | 来源:发表于2020-06-30 14:36 被阅读0次

一、需知

iOS13更新后,通过 present 方式呈现出来的画面,默认呈现样式变更为UIModalPresentationStyle.automatic,表现为浮动效果,且可以通过下拉手势将画面dismiss

需求一:希望iOS13以上present的画面样式跟以前一样显示为全屏效果

实现方式,设置modalPresentationStylefullScreen
1.在创建实例时设置,代码如下:

let vc = ViewController()
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true, completion: nil)

2.或者在初始化方法中设置,代码如下:

// 适用于纯代码构建的UIViewController
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    modalPresentationStyle = .fullScreen
}

// 适用于xib或storyboard构建的UIViewController    
required init?(coder: NSCoder) {
    super.init(coder: coder)
    modalPresentationStyle = .fullScreen
}

需求二:希望iOS13以上保留浮动效果,禁用下拉手势dismiss画面

实现方式:设置isModalInPresentation属性为true
代码跟上面的实现方式一样。

需求三:监测下拉手势,可以在用户下拉时作出反应

此实现方式只适用于present的画面放在UINavigationController
实现方式:
1.设置代理

let vc = ViewController()
let navi = UINavigationController(rootViewController: vc)
navi.presentationController?.delegate = vc
present(vc, animated: true, completion: nil)

2.实现UIAdaptivePresentationControllerDelegate的代理方法func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController)
此方法需同属性isModalInPresentation结合使用,当isModalInPresentation = true时,下拉试图dismiss画面时,才会调用此方法。

override func viewWillLayoutSubviews() {
    // Ensure textView.text is up to date
    textView.text = editedText
    
    // If our model has unsaved changes, prevent pull to dismiss and enable the save button
    let hasChanges = self.hasChanges
    isModalInPresentation = hasChanges
    saveButton.isEnabled = hasChanges
}
...
func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController) {
    // The user pulled down with unsaved changes
    // Clarify the user's intent by asking whether they intended to cancel or save
    print("isModalInPresentation=\(self.isModalInPresentation)")
    confirmCancel(showingSave: true)
}

上面的代码来自官方 Simple Code,请自行下载:
https://developer.apple.com/documentation/uikit/view_controllers/disabling_pulling_down_a_sheet

相关文章

网友评论

      本文标题:iOS13中present画面消除浮动效果,禁用与监测下拉手势

      本文链接:https://www.haomeiwen.com/subject/qjatqktx.html