swift - 未调用 UICollectionView insetForSectionAtIndex

标签 swift uicollectionview uicollectionviewlayout

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

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;
    .....
}

这是一个每轮都有不同数量的单元格的游戏。我想将单元格居中,所以我插入了:

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

 // calculate the insets and return a UIEdgeInsets
}

由于某种原因,该函数从未被调用。否则,该程序运行良好。或者是否有更好的方法使每一轮的细胞居中?

提前致谢。

最佳答案

这会起作用:

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

关于swift - 未调用 UICollectionView insetForSectionAtIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41033409/

相关文章:

ios - 在 iOS7 中重新排序 UICollectionView

swift - 创建后回调: How to run code immediately after ctor?

swift - 在 Swift 中使用 AVAssetWriter 将 AVCaptureVideoDataOutput 保存到电影文件

arrays - Swift append 项未保存

swift - 应用程序在 `layoutAttributesForSupplementaryElement` 处崩溃

ios - UICollectionView 显示不同的单元格取决于方向

Swift 日历 - 移至下周

swift - UITableView高度根据单元格数适配

ios - UITableViewCell 调整大小不流畅

ios - UICollectionView 未从 SDWebImage 加载图像