0

我正面临UICollectionView. 我希望所有单元格都应该有固定的大小。但我无法达到要求。

我想要像这样的输出:实际需求

我得到的是:错误的设计

这是我的代码:

private var totalColumns: Int = 3
private var cellSpace: CGFloat = 10.0
private var width: CGFloat {
    var temp = (CGFloat(self.totalColumns - 1) * self.cellSpace)
    temp = (self.collectionPreference?.frame.size.width ?? SCREEN_WIDTH) - temp
    temp = temp / CGFloat(totalColumns)
    return temp
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: width, height: width)
}

//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
//        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
//    }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return cellSpace
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return cellSpace
}

之前iOS 13,我可以使用相同的代码实现相同的 UI(当时我正在搜索如何根据其内容设计单元格)。而现在iOS 13,我正在努力实现固定大小的 UI ......非常有趣......

我采取了什么:单元格中的子视图

UIImageView约束:UIImageView 约束

ULabel约束:UILabel 约束

谁能有解决方案?

4

1 回答 1

0

尝试这个:

override func viewDidLoad() {
    super.viewDidLoad()

    let cellSize = CGSize(width:80 , height:80) // your cell size

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical //.horizontal
    layout.itemSize = cellSize
    layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
    layout.minimumLineSpacing = 1.0
    layout.minimumInteritemSpacing = 1.0
    collectionPreference.setCollectionViewLayout(layout, animated: true)

    collectionPreference.reloadData()
}
  • 如果您对此有任何问题,请发表评论。
于 2019-12-26T05:31:13.717 回答