2

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
alionthego
  • 8,508
  • 9
  • 52
  • 125

1 Answers1

10

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.

TylerP
  • 9,600
  • 4
  • 39
  • 43
  • 3
    thanks for your suggestion. the scrollView delegate didn't work for me but adding a refresh control was perfect. Just set the refresh control target to endRefreshing and refreshControl tint color to .clear. Works perfectly. Not sure if isModalInPresentation = true not working because of nested in container or not but couldn't get that to work – alionthego Nov 03 '19 at 02:29
  • Hmmm, that's odd that the scroll view delegate way didn't work. I had my table view controller nested inside a navigation controller and it still worked (although like I said it can still _kind of_ be pulled down, just not all the way). – TylerP Nov 03 '19 at 02:39