简体   繁体   中英

UiCollectionView Spacing issue - swift

I am having a spacing issue with my Collection.i Couldn't figure out why. I want the images from the Second row till the last low to be equally spaced. can someone help me to fix this. 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.

    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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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