首页 > 解决方案 > 更改 collectionView 单元格的大小

问题描述

我的数学不太好,所以我想把这个垂直矩形改成水平的,就像下面的小矩形一样,在一行中,我认为约束有问题

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)

在此处输入图像描述

标签: swiftxcodelayoutuicollectionviewconstraints

解决方案


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

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)
}

推荐阅读