简体   繁体   English

迅速uicollectionview单元格之间没有空间,只有单元格之间的屏幕

[英]swift uicollectionview no space between cell and screen only between cells

In my collectionview I have two layouts, one list and one grid (two colum). 在我的collectionview中,我有两种布局,一种列表和一种网格(两个列)。

When I have list activated there is a 2px space between the cell and the screen on each side. 当我激活列表时,单元格和屏幕两侧之间有2px的间隔。

But when I change to grid layout I only get a small space between my cells and not the cells and the screen. 但是,当我更改为网格布局时,单元格之间只有很小的空间,而单元格和屏幕之间没有。 I would like to add a 2px space between the cell and the screen. 我想在单元格和屏幕之间添加2px的空间。

Code for two colum grid: 两个哥伦格的代码:

class GridLayout: UICollectionViewFlowLayout {

    var numberOfColumns: Int = 3

    init(numberOfColumns: Int) {
        super.init()
        minimumLineSpacing = 1
        minimumInteritemSpacing = 1

        self.numberOfColumns = numberOfColumns
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override var itemSize: CGSize {
        get {
            if let collectionView = collectionView {
                let itemWidth: CGFloat = (collectionView.frame.width/CGFloat(self.numberOfColumns)) - self.minimumInteritemSpacing
                let itemHeight: CGFloat = 260.0
                return CGSize(width: itemWidth, height: itemHeight)
            }

            // Default fallback
            return CGSize(width: 100, height: 100)
        }
        set {
            super.itemSize = newValue
        }
    }

    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
        return proposedContentOffset
    }

}

And code for list which works: 以及适用于列表的代码:

class ListLayout: UICollectionViewFlowLayout {

    var itemHeight: CGFloat = 180

    init(itemHeight: CGFloat) {
        super.init()
        minimumLineSpacing = 1
        minimumInteritemSpacing = 1
        self.itemHeight = itemHeight
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override var itemSize: CGSize {
        get {
            if let collectionView = collectionView {
                //Adding -4 to set a 2px space between cell and screen
                let itemWidth: CGFloat = collectionView.frame.width - 4
                return CGSize(width: itemWidth, height: self.itemHeight)
            }

            // Default fallback
            return CGSize(width: 100, height: 100)
        }
        set {
            super.itemSize = newValue
        }
    }

    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
        return proposedContentOffset
    }

}

Change your init of UICollectionViewFlowLayout to this:- 将您的UICollectionViewFlowLayout初始化更改为:-

init(numberOfColumns: Int) {
    super.init()
    minimumLineSpacing = 1
    minimumInteritemSpacing = 1
    self.numberOfColumns = numberOfColumns

    let leftPadding: CGFloat = 2.0
    let rightPadding: CGFloat = 2.0
    sectionInset = UIEdgeInsetsMake(0, leftPadding, 0, rightPadding)
}

And change your itemSize getter method to this:- 并将您的itemSize getter方法更改为:-

get {
        if let collectionView = collectionView {
           let padding: CGFloat = 2.0
           let itemWidth: CGFloat = (collectionView.frame.width/CGFloat(self.numberOfColumns)) - (self.minimumInteritemSpacing+ 2*padding)
           let itemHeight: CGFloat = 260.0
           return CGSize(width: itemWidth, height: itemHeight)
        }

        // Default fallback
        return CGSize(width: 100, height: 100)
}

Below is how you set custom cell spacing in a collectionview. 以下是如何在collectionview中设置自定义单元格间距。 You have to create an extension. 您必须创建一个扩展。

extension ViewController : UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return //whatever you want in CGSize(width:, height:)//
           //so if you want 2px before screen, between cells, and after cell, you would put (self.view.bounds.width - 6) / 2 for width

}

Make sure the "ViewController" that follows extension is the name of your viewcontroller. 确保扩展名后面的“ ViewController”是您的ViewController的名称。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { return UIEdgeInsetsMake(2,2,2,2) }

  - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
      return UIEdgeInsetsMake(2,2,2,2);
  } this code may help u.

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

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