2017-01-24 9 views
0

私のコレクションビューでは、1つのリストと1つのグリッド(2つの列)があります。swif uicollectionviewセル間のスペースとセル間のスペースなし

リストを有効にすると、セルと画面の間にそれぞれ2pxのスペースがあります。

グリッドレイアウトに変更すると、セルと画面の間に小さなスペースしか得られません。私はセルと画面の間に2ピクセルのスペースを追加したいと思います。リストのための

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 
    } 

} 

とコード作品:2つのcolumグリッドの

コードの下

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 
    } 

} 
+0

コレクションビューでcontentInsetプロパティを設定します。 –

答えて

1
これにUICollectionViewFlowLayout のごのinit変更

: -

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

を、これにご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) 
} 
0

あなたはcollectionviewでカスタムセルの間隔を設定する方法です。拡張機能を作成する必要があります。

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 

} 

拡張を次の "のViewController" はあなたのViewControllerの名前であることを確認してください。

0

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. 
関連する問題