CollectionView 不响应触摸事件

CollectionView Not Responding to Touch Events

我在 UIViewController 中有一个 UICollectionView 声明如下:

class CollectionViewController : UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate {

    private var tables = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
    var collectionView : UICollectionView!


    override func viewDidLoad() {

        super.viewDidLoad()


        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0 

        collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsVerticalScrollIndicator = false
        collectionView.backgroundColor = UIColor.clear
        collectionView.register(TableBookingCell.self, forCellWithReuseIdentifier: "TableBookingCell")

        collectionView.isUserInteractionEnabled = true
        self.view.isUserInteractionEnabled = true

        self.view.addSubview(collectionView)

        collectionView.snp.makeConstraints {
            make in

            make.edges.equalTo(view)
        }

    }




    //MARK: - CollectionViewDelegate

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return tables.count
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: self.view.frame.width, height: 80)
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TableBookingCell", for: indexPath) as! TableBookingCell

        cell.isUserInteractionEnabled = true

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        if indexPath.row < self.tables.count {
            print("logging click")
        }
    }  
}

我接下来将这个 CollectionViewController 嵌入到另一个 UIViewController 中,并将其添加到嵌套在 ScrollView 中的 UIView 中,如下所示:

class HomeController: UIViewController, UIScrollViewDelegate {

    private let scrollView = UIScrollView()
    private let contentContainer = UIView()
    private let collectionViewController = CollectionViewController()

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func viewDidLoad() {
        super.viewDidLoad()


        scrollView.contentInsetAdjustmentBehavior = .never
        scrollView.delegate = self


        contentContainer.backgroundColor = .clear


        view.addSubview(scrollView)

        scrollView.addSubview(contentContainer)

        self.addChild(collectionViewController)
        contentContainer.addSubview(collectionViewController.view)

        collectionViewController.didMove(toParent: self)

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
            self.scrollView.contentSize = CGSize(width: self.scrollView.contentSize.width , height: self.collectionViewController.collectionView.contentSize.height + self.scrollView.contentSize.height)
        }

        scrollView.snp.makeConstraints {
            make in

            make.edges.equalTo(view)
        }

        imageContainer.snp.makeConstraints {
            make in

            make.top.equalTo(scrollView)
            make.left.right.equalTo(view)
            make.height.equalTo(imageContainer.snp.width).multipliedBy(0.7)
        }

        infoContainer.snp.makeConstraints {
            make in

            make.top.equalTo(imageView.snp.bottom).offset(-70)
            make.bottom.equalTo(imageView).offset(-10)
            make.left.equalTo(imageView).offset(10)
            make.right.equalTo(imageView).offset(-50)
        }

        imageView.snp.makeConstraints {
            make in

            make.left.right.equalTo(imageContainer)

            //** Note the priorities
            make.top.equalTo(view).priority(.high)

            //** We add a height constraint too
            make.height.greaterThanOrEqualTo(imageContainer.snp.height).priority(.required)

            //** And keep the bottom constraint
            make.bottom.equalTo(imageContainer.snp.bottom)
        }

        contentContainer.snp.makeConstraints {
            make in

            make.top.equalTo(imageContainer.snp.bottom)
            make.left.right.equalTo(view)
            make.bottom.equalTo(scrollView)
        }

        collectionViewController.view.snp.makeConstraints {
            make in

           // make.edges.equalTo(contentContainer).inset(14)


            make.left.right.equalTo(view)
            make.top.equalTo(contentContainer)
            make.bottom.equalTo(view)
        }

    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        scrollView.scrollIndicatorInsets = view.safeAreaInsets
        scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: view.safeAreaInsets.bottom, right: 0)
    }

    //MARK: - Scroll View Delegate

    private var previousStatusBarHidden = false

    func scrollViewDidScroll(_ scrollView: UIScrollView) {

        if previousStatusBarHidden != shouldHideStatusBar {

            UIView.animate(withDuration: 0.2, animations: {
                self.setNeedsStatusBarAppearanceUpdate()
            })

            previousStatusBarHidden = shouldHideStatusBar
        }
    }  
}

但是 UICollectionView 没有响应任何 touchUpInside 事件。

contentContainer 似乎缺少高度限制。如果加上下面的代码,你就会明白我的意思了

   contentContainer.clipsToBounds = true

在这种情况下,您根本看不到collectionView。

添加高度约束并对齐contentContainer和CollectionViewController的底部可以使一切看起来正确。

    contentContainer.heightAnchor.constraint(equalToConstant: 3000).isActive = true


     collectionViewController.view.topAnchor.constraint(equalTo: contentContainer.topAnchor).isActive  = true

       collectionViewController.view.bottomAnchor.constraint(equalTo: contentContainer.bottomAnchor).isActive  = true

        collectionViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive  = true

        collectionViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive  = true

这不是故事的全部,而是专门回答了为什么 collectionView 不响应触摸事件。希望你明白了。