我有一个具有以下声明和构造函数的类:
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/