【问题标题】:changing the size of collectionView cell更改 collectionView 单元格的大小
【发布时间】:2020-09-11 21:24:11
【问题描述】:

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

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)

【问题讨论】:

  • 尝试使用这个内置函数 collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 并相应调整高度和宽度。

标签: swift xcode layout uicollectionview constraints


【解决方案1】:

您似乎在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)
}

【讨论】:

  • areaView.heightAnchor.constraint(equalTo: areaView.widthAnchor, multiplier: 0.5).isActive = true 此行影响大小
  • @mazenqp 你也需要更新你的高度锚。
  • 如果我想将宽度更改为常数,我应该这样做并在 setUpLaout 方法中也给出区域宽度约束
  • 是的,你必须给 sizeForItem 指定宽度。
  • 和约束宽度应该是?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
相关资源
最近更新 更多