因此,正如标题所示,我似乎遇到了一个奇怪的问题。我在这里要做的就是创建一个2列的collectionview,而无需将任何内容硬编码到我的委托方法中。调试后,我发现insetForSectionAt在sizeForItemAt之后被调用,因此在计算每个单元格的大小时不考虑自定义插入。

extension ViewController: UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    print("left: ", flowLayout.sectionInset.left)   // Prints out 0.0
    print("right: ", flowLayout.sectionInset.right) // Prints out 0.0
    let marginsAndInsets = flowLayout.sectionInset.left + flowLayout.sectionInset.right + flowLayout.minimumInteritemSpacing * (cellsPerRow - 1)
    let itemWidth = (collectionView.bounds.size.width - marginsAndInsets) / cellsPerRow
    return CGSize(width: itemWidth, height: itemWidth)
}

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

通过将值直接硬编码到itemWidth变量中(例如-20),可以非常轻松地解决此问题,因为我知道左右inset的值10 + 10
let itemWidth = (collectionView.bounds.size.width - marginsAndInsets - 20) / cellsPerRow

但是,我必须相信有一种更好的方法,如何在UIEdgeInset计算完成后如何调用reloadData以便正确调整单元格的大小?

最佳答案

所以这就是我最终要做的,我只是在为collectionView及其单元格创建尺寸时直接添加了自定义项。

class PinController: UICollectionViewController {

fileprivate let pinCellId = "pinCellId"
fileprivate let cellsPerRow: CGFloat = 2.0
fileprivate let margin: CGFloat = 10.0
fileprivate let topMargin: CGFloat = 2.0
}


extension PinController: UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let flowLayout = customize(collectionViewLayout, margin: margin)
    let itemWidth = cellWidth(collectionView, layout: flowLayout, cellsPerRow: cellsPerRow)
    return CGSize(width: itemWidth, height: itemWidth * 1.1)
}

/*
 Customizes the layout of a collectionView with space to the left and right of each cell that is
 specified with the parameter margin
*/
private func customize(_ collectionViewLayout: UICollectionViewLayout, margin: CGFloat) -> UICollectionViewFlowLayout {
    let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
    // Interitem spacing affects the horrizontal spacing between cells
    flowLayout.minimumInteritemSpacing = margin
    // Line spacing affects the vertical space between cells
    flowLayout.minimumLineSpacing = margin
    // Section insets affect the entire container for the collectionView cells
    flowLayout.sectionInset = UIEdgeInsets(top: topMargin, left: margin, bottom: 0, right: margin)
    return flowLayout
}

/*
 Calculates the proper size of an individual cell with the specified number of cells in a row desired,
 along with the layout of the collectionView
 */
private func cellWidth(_ collectionView: UICollectionView, layout flowLayout: UICollectionViewFlowLayout, cellsPerRow: CGFloat) -> CGFloat {
    // Calculate the ammount of "horizontal space" that will be needed in a row
    let marginsAndInsets = flowLayout.sectionInset.left + flowLayout.sectionInset.right + flowLayout.minimumInteritemSpacing * (cellsPerRow - 1)
    let itemWidth = (collectionView.bounds.size.width - marginsAndInsets) / cellsPerRow
    return itemWidth
}
}

关于ios - UICollectionViewDelegateFlowLayout Edge嵌入无法被sizeForItemAt函数识别?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43165636/

10-12 14:45