改变 collectionView 单元格的大小

changing the size of collectionView cell

我的数学不太好,所以我想将这个垂直矩形更改为水平矩形,如下图所示,在一行中彼此相邻的小矩形我认为约束存在问题

let areaView: UICollectionView = {
       let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
       let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.translatesAutoresizingMaskIntoConstraints = false
        cv.register(customCell.self, forCellWithReuseIdentifier: "cell")
        cv.backgroundColor = UIColor(red: 245, green: 245, blue: 245, a: 1)
       return cv
    }()
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor(red: 245, green: 245, blue: 245, a: 1)
        view.addSubview(areaView)
        areaView.delegate = self
        areaView.dataSource = self
        setUpLayout()
    }


    func setUpLayout(){
        areaView.topAnchor.constraint(equalTo: view.topAnchor, constant: 40).isActive = true
        areaView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        areaView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        areaView.heightAnchor.constraint(equalTo: areaView.widthAnchor, multiplier: 0.5).isActive = true


    }
}

extension Search: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width/2.5, height: collectionView.frame.width/2)
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! customCell
        cell.data = self.data[indexPath.row]
        cell.backgroundColor = UIColor.white
        return cell
    }

}

像这样

如果我将高度更改为 50,它将像这样

return CGSize(width: collectionView.frame.width/2.5, height: 50)

您似乎在 sizeForItem 中提供了错误的 height,请尝试提供:

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

此外,请查看: 更改 sizeForItem 方法以将大小调整为您需要的大小,如下所示:

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

这里我给它的高度是50,你可以给它任何你喜欢的高度。

您也可以通过调整 heightAnchor 来做到这一点:

// areaView heightAnchor like this:-
areaView.heightAnchor.constraint(equalToConstant: 50).isActive = true

// and set sizeForItem to this :-

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