0

私はので、私は次のコードを書いた中央にあることがUICollectionViewの項目を望んでいた:設定UICollectionViewのUIEdgeInsets

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { 
    let collectionViewWidth = collectionView.bounds.width 
    let collectionViewHeight = collectionView.bounds.height 
    let numberOfRows = CGFloat(collectionView.numberOfItemsInSection(0)) 
    let leftValue = (collectionViewWidth/2.0) - (numberOfRows * 5) - (numberOfRows * (collectionViewHeight/2.0)) 
    print(leftValue) 
    return UIEdgeInsets(top: 0, left: leftValue, bottom: 0, right: 0) 
} 

をしかし、今の問題は、私の左のインセットは、してください変更として、私は左にそれをスクロールすることができないです何か解決策を提案してください。

答えて

0

私はコードであるので、ここで、いくつかの計算とすべてを行うことによって、これを考え出した:より詳細なコードの場合

func refreshCollectionView(count: Int) { 
    let collectionViewHeight = collectionView.bounds.height 
    let collectionViewWidth = collectionView.bounds.width 
    let numberOfItemsThatCanInCollectionView = Int(collectionViewWidth/collectionViewHeight) 
    if numberOfItemsThatCanInCollectionView > count { 
     let totalCellWidth = collectionViewHeight * CGFloat(count) 
     let totalSpacingWidth: CGFloat = CGFloat(count) * (CGFloat(count) - 1) 
     // leftInset, rightInset are the global variables which I am passing to the below function 
     leftInset = (collectionViewWidth - CGFloat(totalCellWidth + totalSpacingWidth))/2; 
     rightInset = -leftInset 
    } else { 
     leftInset = 0.0 
     rightInset = -collectionViewHeight 
    } 
    collectionView.reloadData() 
} 

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

あなたがここに私のコードhttps://github.com/anirudha-music/CollectionViewCenter

を見ることができます
関連する問題