未调用UICollectionView insetForSectionAtIndex

Jon*_*lin 7 uicollectionview uicollectionviewlayout swift

我有一个具有以下声明和构造函数的类:

class GameView: UIView, UICollectionViewDelegate,  UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
    var collectionView: UICollectionView?
    var flowLayout =  UICollectionViewFlowLayout()

    init (frame : CGRect, navigationController:UINavigationController, parentViewController: ScrambleViewController)
{
    super.init(frame : frame)
    cvFrame = CGRect(x: cvLeftBorder, y: cvY, width: Int(UIScreen.main.bounds.width) -  (2 * cvLeftBorder), height: cvHeight)
    collectionView = UICollectionView(frame: cvFrame!, collectionViewLayout: flowLayout);
    collectionView!.dataSource = self
    collectionView!.delegate = self;
    .....
}
Run Code Online (Sandbox Code Playgroud)

这是一种每轮都有不同数量的细胞的游戏.我想将细胞居中,所以我插入:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {

 // calculate the insets and return a UIEdgeInsets
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,永远不会调用该函数.否则,程序运行正常.或者是否有更好的方法让细胞在每一轮中心?

提前致谢.

Eyz*_*uky 11

这将有效:

@objc(collectionView:layout:insetForSectionAtIndex:)  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{
    return UIEdgeInsetsMake(5, 5, 5, 5)
}
Run Code Online (Sandbox Code Playgroud)


Mic*_*ith 0

Swift 3 中的签名发生了变化:

@available(iOS 6.0, *)
optional public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
Run Code Online (Sandbox Code Playgroud)