ios - UiCollectionView 间距问题 - swift

标签 ios swift xcode uicollectionview

我的收藏集有间距问题。我不知道为什么。我希望从第二行到最后一个低点的图像间隔相等。有人可以帮我解决这个问题吗?谢谢。

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
    }
}

看起来像这样

enter image description here

实际输出

enter image description here

最佳答案

你需要相应地设置布局,现在你的布局会根据屏幕尺寸改变所以你需要根据一个屏幕尺寸计算然后它会在其他屏幕尺寸上工作:

现在,布局将像这样,在这 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)
        }
    }

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

enter image description here

关于ios - UiCollectionView 间距问题 - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50663688/

相关文章:

尝试在另一个 Storyboard中打开时 iOS SideMenu 崩溃

ios - 理解 SpriteKit 中的重力和向量

ios - SwiftUI 中的 textFieldDidBeginEditing 和 textFieldDidEndEditing

xcode - 为什么 npm install 由于 Xcode 而失败?

ios - iOS中的数据模型版本编译器(MOMC)错误

ios - 有没有办法显示 FB 对话框并在 Post 上调用我自己的方法?

iphone - 在 NIAttributedLabel 中设置链接属性

swift - 如何在 SwiftUI 中向 Viewmodifiers 添加闭包?

ios - 如何在 Swift 中将对象数组发送到服务器?

ios - 带有 Xcode 的 iOS 的链接器标志