1

I am new in swift, here is a solution to make a CollectionView with 4 cell in screen and in below line I said 15 margin for each side this working for left and right but this not working for top and button

return UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)

, How could we have same margin on top and bottom ?

Here is MainViewController :

import UIKit

class MainViewController: UIViewController , UICollectionViewDelegate, UICollectionViewDataSource {

    var listArray : [String] = []
    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        print("MainViewController")
        collectionView.delegate = self
        collectionView.dataSource = self

        for index in 1 ..< 12 {
            listArray.append("Cell \(index)")
        }
    }


    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return listArray.count
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mainCell", for: indexPath) as! MainCollectionViewCell
        let cellIndex = indexPath.item
        cell.cellLabelText.text = listArray[cellIndex]
        cell.backgroundColor = UIColor.blue

        return cell
    }


}


extension MainViewController : UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let bounds = collectionView.bounds

        return CGSize(width: (bounds.width / 2 ) - 30   , height:  ( bounds.height / 2 ) - 30   )
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

}

Here is MainCollectionViewCell :

import UIKit

class MainCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var cellLabelText: UILabel!

}

enter image description here

Ashley Mills
  • 41,127
  • 14
  • 115
  • 144
PPPPPPPPP
  • 622
  • 7
  • 14
  • Possible duplicate of [UICollectionView spacing margins](https://stackoverflow.com/questions/13970950/uicollectionview-spacing-margins) – Kuldeep Oct 01 '18 at 10:16

4 Answers4

3

Since your collection view has only 1 section, you need to size the spacing between the different rows/columns inside a section and different items in the same row/column correctly rather than setting an inset for the sections.

UICollectionViewDelegateFlowLayout has the following two methods you should implement to achieve your goals: collectionView(_:layout:minimumLineSpacingForSectionAt:) and collectionView(_:layout:minimumInteritemSpacingForSectionAt:).

To copy the spacing you currently use for sections, you need to implement those methods like below:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return CGFloat(15)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return CGFloat(15)
}
Dávid Pásztor
  • 40,247
  • 8
  • 59
  • 80
1

You should set "sectionInset" property of UICollectionViewFlowLayout like this

if let flowLayout = yourCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        flowLayout.sectionInset = UIEdgeInsetsMake(100, 0, 100, 0)
    }

This will add top and bottom margins by 100 points in your collection view. By the way the sequence is top, left, bottom, right in UIEdgeInsetsMake method.

Noshaid Ali
  • 105
  • 11
0

Select your storyboard:

1) Select UICollection view

2) Set Section Insets for TOP and BOTTOM

enter image description here

I hope this could help!!

Dheeraj D
  • 3,651
  • 4
  • 16
  • 33
0

Try this

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForItemAt indexPath: IndexPath) -> UIEdgeInsets {

                return UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
            }
piyush ranjan
  • 333
  • 4
  • 12