Andres
Andres

Reputation: 11757

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:

But those two are not the same scenario.

Upvotes: 3

Views: 1311

Answers (1)

Aditya Vyavahare
Aditya Vyavahare

Reputation: 53

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)
    ])
}
}

Upvotes: 2

Related Questions