【问题标题】:UiCollectionView Spacing issue - swiftUiCollectionView 间距问题 - 快速
【发布时间】:2018-06-03 05:57:58
【问题描述】:

我的 Collection 存在间距问题。我不知道为什么。我希望从第二行到最后一个低点的图像等间距。有人可以帮我解决这个问题。 tnx。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if (indexPath.row == 0) {
        return   singleUserModel.isevenItems(totalCount: CurrentUser.items.count) ? CGSize(width: self.view.frame.width * 0.4 , height: self.view.frame.height / 4) : CGSize(width: self.view.frame.width , height: self.view.frame.height / 4)
    } else {
        return CGSize(width:  self.view.frame.width * 0.4, height: self.view.frame.height / 4)
    }
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return CurrentUser.items.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UserDetailCell", for: indexPath) as! UserDetailCell
    configureCell(cell: cell, forRowAtIndexPath: indexPath as NSIndexPath)
    return cell
}

func configureCell(cell: UserDetailCell, forRowAtIndexPath indexPath: NSIndexPath) {
    DispatchQueue.global(qos: .background).async {
        cell.ItemImage.image = self.singleUserModel.loadImage(imageUrl: self.CurrentUser.items[indexPath.row])
        cell.activityIndicator.isHidden = true
    }
}

查看看起来像这样

实际输出

【问题讨论】:

    标签: ios swift xcode uicollectionview


    【解决方案1】:

    您需要相应地设置布局,现在您的布局将根据屏幕尺寸进行更改所以您需要根据一个屏幕尺寸计算,然后它将适用于其他屏幕尺寸:

    现在,布局将是这样的,在这 2 个图像中将显示宽度,如果您想设置左右两侧的空间,请相应地设置布局宽度。

        func collectionView(_ collectionView: UICollectionView, layout 
    
    collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            if (indexPath.row == 0) {
                return   singleUserModel.isevenItems(totalCount: CurrentUser.items.count) ? CGSize(width: self.view.frame.width * 0.4 , height: self.view.frame.height / 4) : CGSize(width: self.view.frame.width , height: self.view.frame.height / 4)
            } else {
                return CGSize(width:  (self.view.frame.width/2), height: self.view.frame.height / 4)
            }
        }
    

    删除单元格和线条的最小间距,部分插图也将为零,如下图所示:

    【讨论】:

    • 您需要将单元格和行的最小间距设置为 0,并且部分插入将为 0
    • Bydeault 最小间距为 0,因此您需要设置 0,然后我的代码将为您工作。我已经更新了 mu 答案请检查
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多