CollectionView不响应触摸事件

时间:2019-02-19 00:38:12

标签: ios swift uicollectionview scrollview touch-up-inside

我在声明为UICollectionView的内部有一个UIViewController,如下所示:

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内,并添加它的视图以使其位于嵌套在UIView中的ScrollView内,如下所示:

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事件。

1 个答案:

答案 0 :(得分:1)

似乎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无法响应触摸事件。希望你明白了。