Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid pull down on UITableView to dismiss presented modal in UIModalPresentationStyle.pageSheet

I have a view controller with a child view controller inside. This child view controller is a UITableView.

When I present the view controller modally using the UIModalPresentationStyle.pageSheet and the user drags down the UITableView the whole view controller is dragged down and it ends up being dismissed.

I'm looking for a way to disable that gesture in the UITableView. I found several posts on SO recommending to use isModalInPresentation = true or just use .fullScreen as UIModalPresentationStyle but that's not what I need. I want the user to be able to dismiss the view controller with a gesture if the user drags down the presented view controller from the navigation bar but not from the UITableView

I've already checked:

  • ios13 prevent pulling down on tableView which is scrolled to top from dismissing sheet style modally presented viewController
  • Disable gesture to pull down form/page sheet modal presentation

But those two are not the same scenario.

like image 382
Andres Avatar asked Nov 17 '25 13:11

Andres


1 Answers

Instead of declaring another view controller, use a UIView in the MainViewController and present that UIView modally with appropriate constraints and relevant animations for sliding up and down. Doing it this way will prevent your modal view from sliding down when you get to the top of the table view.

class MapViewController: UIViewController {
// The view that goes up and down
let modalView = UIView()
// Constants for the position of the modal view
let bottomStopPosition: CGFloat = UIScreen.main.bounds.height - 80
let topStopPosition: CGFloat = 100

// Gesture recognizer for dragging
let panGesture = UIPanGestureRecognizer()

override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(modalView)
// Add a pan gesture recognizer to the modal view
panGesture.addTarget(self, action: #selector(handlePan(_:)))
modalView.addGestureRecognizer(panGesture)
configureTableView()
}
}

extension MapViewController: UITableViewDelegate, UITableViewDataSource {
func configureTableView() {
    // Set data source and delegate
    tableView.dataSource = self
    tableView.delegate = self
    tableView.isUserInteractionEnabled = true
    // Register your custom cell class
    tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "CustomCell")
    
    // Add the UITableView to the view
    view.addSubview(tableView)
    
    // Configure constraints
    tableView.translatesAutoresizingMaskIntoConstraints = false
    
    // Ensure titleBarView is added as a subview before configuring constraints
    // You should have code that adds titleBarView before this point
    
    // Add constraints for the UITableView
    NSLayoutConstraint.activate([
        // Align the top of the tableView to the bottom of titleBarView
        tableView.topAnchor.constraint(equalTo: titleBarView.bottomAnchor),
        
        // Make sure the tableView fills the remaining vertical space
        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
    ])
}
}
like image 61
Aditya Vyavahare Avatar answered Nov 20 '25 06:11

Aditya Vyavahare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!