Disable gesture to pull down form/page sheet modal presentation

前端 未结 14 2815
我寻月下人不归
我寻月下人不归 2020-11-27 13:58

In iOS 13 modal presentations using the form and page sheet style can be dismissed with a pan down gesture. This is problematic in one of my form sheets because the user dra

14条回答
  •  攒了一身酷
    2020-11-27 14:31

    This gesture can be found in the modal view controller's presentedView property. As I debugged, the gestureRecognizers array of this property has only one item and printing it resulted in something like this:

    UIPanGestureRecognizer: 0x7fd3b8401aa0 (_UISheetInteractionBackgroundDismissRecognizer);

    So to disable this gesture you can do like below:

    let vc = UIViewController()
    
    self.present(vc, animated: true, completion: {
      vc.presentationController?.presentedView?.gestureRecognizers?[0].isEnabled = false
    })
    

    To re-enable it simply set isEnabled back to true:

    vc.presentationController?.presentedView?.gestureRecognizers?[0].isEnabled = true
    

    Note that iOS 13 is still in beta so a simpler approach might be added in an upcoming release.

    Although this solution seems to work at the moment, I would not recommend it as it might not work in some situations or might be changed in future iOS releases and possibly affect your app.

提交回复
热议问题