xcode 从左到右的单元格集合未正确显示

ulydmbyx  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(131)

集合视图单元格从左到右显示不正确。它在中心有差距。我如何纠正此问题。

collectionView_.register(UINib.init(nibName: String(describing: GoalSelectionCollectionViewCell.self), bundle: nil), forCellWithReuseIdentifier: reuseIdentifier)
collectionView_.allowsMultipleSelection = true
    
if let flowLayout = collectionView_?.collectionViewLayout as? UICollectionViewFlowLayout {

    flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
}

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

    let itemSize:CGSize = CGSize(width: 80.0, height: 30.0);

    return itemSize;
}

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

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

aor9mmx11#

您需要符合UICollectionViewDelegateFlowLayout,还需要设置minimumLineSpacing(行之间的最小间距)和minimumInteritemSpacing(同一行中项目之间的最小间距)

相关问题