我有一个视图控制器,里面有一个子视图控制器。这个子视图控制器是一个
UITableView
。
当我使用
UIModalPresentationStyle.pageSheet
以模态方式呈现视图控制器并且用户向下拖动 UITableView
时,整个视图控制器将被向下拖动并最终被关闭。
我正在寻找一种方法来禁用
UITableView
中的该手势。我发现 SO 上有几篇文章建议使用 isModalInPresentation = true
或仅使用 .fullScreen
作为 UIModalPresentationStyle
但这不是我需要的。如果用户从导航栏而不是从 UITableView
向下拖动呈现的视图控制器,我希望用户能够通过手势关闭视图控制器
我已经检查过:
但这两个不是同一个场景。
不要声明另一个视图控制器,而是在 MainViewController 中使用 UIView 并以适当的约束和相关动画来模态地呈现该 UIView 以进行上下滑动。 这样做可以防止当您到达表格视图顶部时模态视图向下滑动。
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)
])
}
}