Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何减少UICollection视图中的垂直间距_Ios_Swift_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios 如何减少UICollection视图中的垂直间距

Ios 如何减少UICollection视图中的垂直间距,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我正在使用UICollectionView下面是我的代码,如何删除垂直间距? 我尝试在UICollectionViewFlowLayout中设置,但没有成功 class ViewController: UIViewController { @IBOutlet weak var cv: UICollectionView! override func viewDidLoad() { super.viewDidLoad() cv.re

我正在使用
UICollectionView
下面是我的代码,如何删除垂直间距? 我尝试在
UICollectionViewFlowLayout
中设置,但没有成功

class ViewController: UIViewController {
    
    @IBOutlet weak var cv: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        let xx : CGFloat = (UIScreen.main.bounds.width / 8)
        let yy : CGFloat = 6.0
        layout.itemSize = CGSize(width: xx, height: yy)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 1
        cv!.collectionViewLayout = layout
    }
}

extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 70
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        if (indexPath.row/2 == 0) {
            cell.backgroundColor = UIColor.blue.withAlphaComponent(12)
        }
        else if (indexPath.row/4 == 0) {
            cell.backgroundColor = UIColor.blue.withAlphaComponent(0.5)
        }
        else{
            cell.backgroundColor = UIColor.blue.withAlphaComponent(0.1)
        }
        return cell
    }
}

您可以使用
UICollectionViewDelegateFlowLayout
protcol和
minimuminetemspacingforSectionat
minimumLineSpacingForSectionAt
函数,如以下代码:

extension ViewController: UICollectionViewDelegateFlowLayout {
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10 // or any value
    }
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10 // or any value
    }
}

在您的案例中,间距主要是由每个项目的大小-宽度-引起的

因此,您有以下代码来计算单个项目的宽度:

let xx : CGFloat = (UIScreen.main.bounds.width / 8)
但是,由于您有剖面插图,您的计算必须更改,或者必须删除插图,因此:

let xx : CGFloat = (UIScreen.main.bounds.width - 10 / 8)
// 10 = 5 left + 5 right is taken for section insets
或者,删除部分插图:

// layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
如果您想给出前导/尾随边距,请改用集合约束,即将
collectionView
约束为其superview的
leadingAnchor
/
trailingAnchor
的常量(例如5),则无需进行计算,由于您可以使用
sizeForItem
delegate方法并通过以下方式返回计算:

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.bounds.width / 8, height: 5.0)
 }