Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.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 单元格之间的间距不正确UICollectionView_Ios_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios 单元格之间的间距不正确UICollectionView

Ios 单元格之间的间距不正确UICollectionView,ios,uicollectionview,uicollectionviewcell,Ios,Uicollectionview,Uicollectionviewcell,我有一个UICollectionView,其中包含UICollectionViewCells,其中包含用于内容的UIButtons 但是,每个单元格之间的间距不正确。集合视图中的单元格大小不特定,因为它应该是一个标签列表,标签的长度明显不同 下面是它的样子: 每个单元格之间的水平间距完全不正确,因为它们每行都不同 单元格的代码: class ViewController: UIViewController, UICollectionViewDataSource, UICollectionView

我有一个UICollectionView,其中包含UICollectionViewCells,其中包含用于内容的UIButtons

但是,每个单元格之间的间距不正确。集合视图中的单元格大小不特定,因为它应该是一个标签列表,标签的长度明显不同

下面是它的样子:

每个单元格之间的水平间距完全不正确,因为它们每行都不同

单元格的代码:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    @IBOutlet weak var wordsCollection: UICollectionView!
    var items = ["test", "this", "word view", "like", "collection", "testing", "give", "this", "testing", "test", "test", "this", "word view", "like", "collection", "testing", "give", "this", "testing", "test", "test", "this", "word view", "like", "collection"]
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.items.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "wordCell", for: indexPath as IndexPath) as! WordCell
        cell.wordButton.setTitle(items[indexPath.row], for: UIControlState.normal)
        cell.wordButton.backgroundColor = UIColor.gray
        if(indexPath.row % 3 == 0){
            cell.wordButton.backgroundColor = UIColor.gray
        }
        cell.wordButton.clipsToBounds = true
        cell.wordButton.layer.cornerRadius = 2
        cell.wordButton.sizeToFit()

        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        if let flowLayout = wordsCollection.collectionViewLayout as? UICollectionViewFlowLayout { flowLayout.estimatedItemSize = CGSize(width:1, height:1) }
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
语言为Swift 4,Im以iOS 10.3为目标。

实现:UICollectionViewDelegateFlowLayout

然后:

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

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

您尚未实现UICollectionview的流布局委托,然后实现interItemSpacing和MinimumLineSpacing方法感谢您的建议!刚刚给了它一个机会,它已经进步了,但它仍然有一些排。你想把它们对齐吗?否则它只会尝试填充行,我猜您在单元格中使用autalayout,尝试将前导/尾随的优先级设置为500,但这没有任何区别。我认为单元格已启用自动布局,因为flowLayout.estimatedItemSize=CGSizewidth:1,高度:1在我的viewDidLoad func中。抱歉,我认为在您的WordCell中存在一些混淆,标签上的前导和尾随约束优先级为500,不用担心!是的,对不起,我是说我试过500号优先权标签上的限制条件