2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UICollectionViewで動的にマージンを変更したいとき、cellのサイズをstoryboardの設定値から取ってくる

Last updated at Posted at 2016-03-01
  • 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 のなかに色々入ってました。これを使って正しいマージンを計算するとデバイスごとに異なるマージンで出力できますね。
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?