简体   繁体   中英

How could we add margin in bottom and top of UICollectionView?

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!

}

在此输入图像描述

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)
}

Select your storyboard:

1) Select UICollection view

2) Set Section Insets for TOP and BOTTOM

在此输入图像描述

I hope this could help!!

Try this

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

                return UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
            }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM