Cell spacing in UICollectionView

后端 未结 26 3599
旧巷少年郎
旧巷少年郎 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:38

    My solution in Swift 3 cell line spacing like in Instagram:

    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.rgb(red: 227, green: 227, blue: 227)
        cv.showsVerticalScrollIndicator = false
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 1
        layout.minimumInteritemSpacing = 1
        return cv
    }()
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
        switch UIDevice.current.modelName {
        case "iPhone 4":
            return CGSize(width: 106, height: 106)
        case "iPhone 5":
            return CGSize(width: 106, height: 106)
        case "iPhone 6,7":
            return CGSize(width: 124, height: 124)
        case "iPhone Plus":
            return CGSize(width: 137, height: 137)
        default:
            return CGSize(width: frame.width / 3, height: frame.width / 3)
        }
    }
    

    How to detect device programmaticlly: https://stackoverflow.com/a/26962452/6013170

提交回复
热议问题