Cell spacing in UICollectionView

后端 未结 26 3282
旧巷少年郎
旧巷少年郎 2020-11-22 15:53

How do I set cell spacing in a section of UICollectionView? I know there is a property minimumInteritemSpacing I have set it to 5.0 still the spaci

26条回答
  •  悲哀的现实
    2020-11-22 16:32

    Storyboard Approach

    Select CollectionView in your storyboard and go to size inspector and set min spacing for cells and lines as 5

    Swift 5 Programmatically

    lazy var collectionView: UICollectionView = {
            let layout = UICollectionViewFlowLayout()
            layout.scrollDirection = .horizontal
    
            //Provide Width and Height According to your need
            let width = UIScreen.main.bounds.width / 4
            let height = UIScreen.main.bounds.height / 10
            layout.itemSize = CGSize(width: width, height: height)
    
            //For Adjusting the cells spacing
            layout.minimumInteritemSpacing = 5
            layout.minimumLineSpacing = 5
    
            return UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        }()
    

提交回复
热议问题