ios 避免在UIModalPresentationStyle.pageSheet中下拉可移除视图以关闭呈现的模态

lnvxswe2  于 12个月前  发布在  iOS
关注(0)|答案(1)|浏览(193)

我有一个视图控制器,里面有一个子视图控制器。这个子视图控制器是UITableView
当我使用UIModalPresentationStyle.pageSheet模式化地呈现视图控制器时,用户向下拖动UITableView,整个视图控制器被向下拖动,并最终被解散。
我正在寻找一种方法来禁用该手势在UITableView。我在SO上发现了几篇推荐使用isModalInPresentation = true或仅使用.fullScreen作为UIModalPresentationStyle的帖子,但这不是我需要的。如果用户从导航栏而不是从UITableView下拉显示的视图控制器,我希望用户能够通过手势关闭视图控制器
我已经查过了:

但这两个不是同一个场景。

hjzp0vay

hjzp0vay1#

不要声明另一个视图控制器,而是在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)
    ])
}
}

相关问题