ios13 prevent pulling down on tableView which is scrolled to top from dismissing sheet style modally presented viewController [duplicate]

别来无恙 提交于 2020-01-16 09:01:08

问题


I have a tableViewController which is presented modally with the default sheet style presentation.

I want to keep this modal style as it looks good and works well in my app. And I like the dismiss when pulling down on the navigation bar. However what I don't want is pulling down on the tableView cells to cause the tableViewController to be dismissed when the tableView is already scrolled to the top.

Is there anyway to inhibit this behaviors but keep the sheet style modal presentation? I want the pull down on the tableView to keep the vertical bounce effect and only to be able to dismiss the modally presented tableViewController through pan by pulling down on the navigation bar portion.


回答1:


You can disable the pull-to-dismiss behavior by setting isModalInPresentation to true on your table view controller when the user begins dragging on the table view, and then reset it back to false when they stop dragging, like so:

class YourTableViewController: UITableViewController {
    override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        isModalInPresentation = true
    }

    override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        isModalInPresentation = false
    }
}

Note that you'll still be able to slightly pull down your table view controller, but at least you won't be able to dismiss it entirely. And since the value is set back to false when dragging stops, you'll be able to dismiss by pulling down on the navigation bar.

Also, if you add a UIRefreshControl to your table view, it disables the pull-to-dismiss behavior when pulling down on the table view.



来源:https://stackoverflow.com/questions/58676063/ios13-prevent-pulling-down-on-tableview-which-is-scrolled-to-top-from-dismissing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!