添加到视图时,CollectionView不滚动

问题描述

这是我的代码

let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = .greatestFiniteMagnitude
layout.minimumLinespacing = 15
layout.scrollDirection = .horizontal
layout.itemSize = CGSize(width: screenWidth*0.2,height: screenHeight*0.15)
       
collectionView = UICollectionView(frame: CGRect(x: 0,y: 0,width: screenWidth*0.9,height: screenHeight*0.15),collectionViewLayout: layout)
collectionView?.layer.zPosition = 10
collectionView?.register(PlantSnapshotCell.self,forCellWithReuseIdentifier:PlantSnapshotCell.identifier)
collectionView?.backgroundColor = .clear
collectionView?.alwaysBounceHorizontal = true
collectionView?.bounces = true
collectionView?.showsHorizontalScrollIndicator = false
       
collectionView?.dataSource = self
collectionView?.delegate = self
       
scrollView.addSubview(collectionView!)
view.addSubview(scrollView)

它在视图中呈现,但是任何形式的交互都不起作用。 collectionView的宽度是超级视图的宽度的90%。水平滚动不起作用。

解决方法

为什么要将collectionView作为子视图添加到scrollView