52474
## 質問

どうして左寄せになる場合と、中央寄せになる場合があるのでしょうか？  
下記にそうなってしまったコードを載せます。

## 左寄せ

```

    import UIKit
    class ViewController: UIViewController {
        @IBOutlet weak var collectionView: UICollectionView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.collectionView.delegate = self
            self.collectionView.dataSource = self
            let layout = UICollectionViewFlowLayout()
            layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            layout.minimumInteritemSpacing = 0
            layout.minimumLineSpacing = 0
            self.collectionView.collectionViewLayout = layout
        }
    }
    
    extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 3
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "hogeId", for: indexPath)
            cell.backgroundColor = indexPath.row % 2 == 0 ? UIColor.green : UIColor.blue
            return cell
        }
    
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
            return CGSize(width: self.collectionView.frame.width / 2, height: 100.0)
        }
    }
    
```

## 中央寄せ

```

    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var collectionView: UICollectionView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.collectionView.delegate = self
            self.collectionView.dataSource = self
            let layout = UICollectionViewFlowLayout()
            layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            layout.minimumInteritemSpacing = 0
            layout.minimumLineSpacing = 0
            self.collectionView.collectionViewLayout = layout
        }
    }
    
    extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 2
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "hogeId", for: indexPath)
            cell.backgroundColor = indexPath.row % 2 == 0 ? UIColor.green : UIColor.blue
            return cell
        }
    
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
            if indexPath.row == 0 {
                return CGSize(width: self.collectionView.frame.width, height: 100.0)
            } else {
                return CGSize(width: self.collectionView.frame.width / 2, height: 100.0)
            }
        }
    }
    
```

## シミュレータの実行結果画像

[![画像の説明をここに入力](https://i.stack.imgur.com/WLKiY.png)](https://i.stack.imgur.com/WLKiY.png)  
[![画像の説明をここに入力](https://i.stack.imgur.com/s0BQ0.png)](https://i.stack.imgur.com/s0BQ0.png)
CC BY-SA 4.0
2018-11-22T07:08:12.637
50534
2019-02-01T07:17:06.353
9008
question
ios swift4 uicollectionview
UICollectionViewのセルが左寄せになるときと中央寄せになるときがある
`UICollectionViewFlowLayout`  
で対応する必要があるようでした。

<https://github.com/mischa-hildebrand/AlignedCollectionViewFlowLayout>  
を使うことで`UICollectionViewFlowLayout`を直接使うよりは少ないコード数で対応可能でした。
CC BY-SA 4.0
2019-02-01T07:17:06.353
52474
2019-02-01T07:17:06.353
9008
50534
answer