【问题标题】:How to automatically resize cells to fit 4 per line [duplicate]如何自动调整单元格大小以适合每行 4 个 [重复]
【发布时间】:2017-04-24 15:13:11
【问题描述】:

我有一个集合视图,我正在寻找一种方法来找出每个单元格可以有多大以适应每行 4 个单元格(在特定的屏幕尺寸上)并在所有单元格上使用该尺寸。我有一个可重复使用的单元格,可用于其余单元格。

【问题讨论】:

    标签: ios swift xcode swift3 uicollectionviewcell


    【解决方案1】:
    extension LevelSelectController: UICollectionViewDelegateFlowLayout {
    
    // Collection view flow layout setup
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        // Compute the dimension of a cell for an NxN layout with space S between
        // cells.  Take the collection view's width, subtract (N-1)*S points for
        // the spaces between the cells, and then divide by N to find the final
        // dimension for the cell's width and height.
    
        let cellsAcross: CGFloat = 4
        let spaceBetweenCells: CGFloat = 0
        let dim = (collectionView.bounds.width - (cellsAcross - 1) * spaceBetweenCells) / cellsAcross
    
        return CGSize(width: dim, height: dim)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(0, 0, 0.0, 0.0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0.0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0.0
    }
    

    【讨论】:

      猜你喜欢
      • 2011-07-07
      • 2010-10-27
      • 1970-01-01
      • 2017-04-10
      • 2014-06-10
      • 2017-03-12
      • 2021-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多