1

How to make vertical uiCollectionViewCell Left side when CollectionView has only one item My collectionView looks like

I want to make my CollectionView like this. How can I do make like this

2 Answers2

2
 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    collectionviw.delegate = self
    collectionviw.dataSource = self
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 20, left: 5, bottom: 10, right: 2)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    collectionviw!.collectionViewLayout = layout
    collectionviw.reloadData()
}

extension ViewController: UICollectionViewDelegate, 
 UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
{
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection 
section: Int) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt i 
 ndexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionviw.dequeueReusableCell(withReuseIdentifier: "cell", 
for: indexPath)

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout 
  collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: 
 IndexPath) -> CGSize {
          let collectionWidth = collectionView.bounds.width / 2 - 50
          return CGSize(width: collectionWidth, height: collectionWidth)
      }
  }

output

Ruchi
  • 109
  • 2
1

I think this is a bug in iOS that automatically centers the single cell in CollectionView because of UICollectionViewFlowLayoutAutomaticSize. You can create your own UICollectionViewFlowLayout

I followed the approach given here :

Stop Single UICollectionView cell Flowing to the centre of the screen

rohit
  • 103
  • 1
  • 7