-
UICollectionViewFlowLayoutを利用していて、セルごとのマージンをデバイスによって変えたりしたい場合の話です。
-
この方法で、厳密にはstoryboardで設定した値というか、現在のレイアウトで指定されている値が取得できます。
-
なにげにハマってしまったのでメモです。
-
下のようなデリゲートメソッドでセルごとの最小マージンを指定するのですが
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
guard let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout else {
return 100 //テキトー
}
print(flowLayout.itemSize.width, flowLayout.sectionInset.left)
return 1000 //テキトー
}
- UICollectionViewLayout を UICollectionViewFlowLayout にキャストした flowLayout のなかに色々入ってました。これを使って正しいマージンを計算するとデバイスごとに異なるマージンで出力できますね。