简体   繁体   English

uiCollectionView间距问题-迅速

[英]UiCollectionView Spacing issue - swift

I am having a spacing issue with my Collection.i Couldn't figure out why. 我的Collection出现间距问题。我不知道为什么。 I want the images from the Second row till the last low to be equally spaced. 我希望从第二行到最后一个低点的图像均匀分布。 can someone help me to fix this. 有人可以帮我解决这个问题吗? tnx. 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
    }
}

View look Like this 视图看起来像这样

在此处输入图片说明

Actual Output 实际产量

在此处输入图片说明

You need to set layout accordingly, Now your layout will be changed according to screen size So you need to calculate according to one screen size then it will work in other screen sizes: 您需要相应地设置布局,现在您的布局将根据屏幕尺寸进行更改,因此您需要根据一种屏幕尺寸进行计算,然后将其用于其他屏幕尺寸:

Now, the layout will like this, In this 2 image will be shown in the width and if you want to set space from left and right side then set layout width accordingly. 现在,布局将像这样。在这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)
        }
    }

Remove minimins spacing for cell and lines and section inset will also Zero like below image: 删除单元格和线条的最小间距,截面插图也将为零,如下图所示:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM