2016-10-19 13 views
7

UICollectionViewFlowLayoutまたはUICollectionViewLayoutを実装しようとしています。layoutAttributesForItemは決して呼び出されません。UICollectionViewLayoutとメソッドlayoutAttributesForItemは決して呼び出されません

からself.layoutAttributesForItemに電話することができます。私はあなたが見ることができPlaygroundプロジェクトを作成している

this flowout例と同様

。全体的に私はちょうど中央のビューをスケールアップしようとしています。

+0

を、それを使用してみてください私はそれが非常に些細な質問だ知っているが、私はそれを聞いてみろ。あなたのコレクションビューのレイアウトにカスタムクラスを正しく割り当てましたか? – Adeel

+0

質問してくれてありがとう:-)時間があれば、このPlaygroundを見てみましょう:https://github.com/JCzz/CollectionView全体的に見て、画面の中央にViewを拡大しようとしています。 –

+0

@ ChrisG.私は同じ問題を抱えています。 "LayoutAttributesForItem"がまったく起動しないことを除いて、すべてうまくいくようです。なぜ私は分からない。 – jnel899

答えて

1

サブクラス化UICollectionViewFlowLayout

によって
let flowLayout = LeftAgignedFlowLayout() 
flowLayout.minimumLineSpacing = 18 
flowLayout.minimumInteritemSpacing = 8 
flowLayout.scrollDirection = .vertical 
self.collectionViewLayout = flowLayout 



class LeftAgignedFlowLayout : UICollectionViewFlowLayout {   

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 
     let arr = super.layoutAttributesForElements(in: rect)! 
     return arr.map { 
      atts in 

      var atts = atts 
      if atts.representedElementCategory == .cell { 
       let ip = atts.indexPath 
       atts = self.layoutAttributesForItem(at:ip)! 
      } 
      return atts 
     } 
    } 

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { 
     var atts = super.layoutAttributesForItem(at:indexPath)! 
     if indexPath.item == 0 { 
      return atts 

     } 
     if atts.frame.origin.x - 1 <= self.sectionInset.left { 
      return atts 

     } 
     let ipPv = IndexPath(item:indexPath.row-1, section:indexPath.section) 
     let fPv = self.layoutAttributesForItem(at:ipPv)!.frame 
     let rightPv = fPv.origin.x + fPv.size.width + self.minimumInteritemSpacing 
     atts = atts.copy() as! UICollectionViewLayoutAttributes 
     atts.frame.origin.x = rightPv 
     return atts 
    } 

} 
+0

ここで本当に提案しているのは、 'UICollectionViewFlowLayout'に頼る代わりに、' layoutAttributesForItem(at:) 'を自分で呼び出してコールバックすることです。 :/ –

+0

@MohammadAbdurraafayはい、カスタムフローレイアウトを使用している場合は、このオーバーライド関数を呼び出します。 –

+0

これは、迅速にlayoutAttributesForItemのインターネット上で(私が見つけることができる)最良の例です。 –

関連する問題