2016-08-30 1 views
4

UICollectionViewCellの幅を動的に設定したいと思います。 私は素早くいくつかのコードを見つけました。Objective-Cのコードが必要です。以下のようなコンテナビューの割合にUICollectionViewCellの動的な幅を作る方法

let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout 
    if indexPath.item % 3 == 0 { 
     let cellWidth = (CGRectGetWidth(collectionView.frame) - (flowLayout.sectionInset.left + flowLayout.sectionInset.right)) 
     return CGSize(width: cellWidth, height: cellWidth/2) 
    } else { 
     let cellWidth = (CGRectGetWidth(collectionView.frame) - (flowLayout.sectionInset.left + flowLayout.sectionInset.right) - flowLayout.minimumInteritemSpacing)/2 
     return CGSize(width: cellWidth, height: cellWidth) 
    } 

答えて

3

を使用することができます...

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 

    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*) collectionView.collectionViewLayout; 

    if (indexPath.item % 3 == 0) { 
     float cellWidth = (CGRectGetWidth(collectionView.frame) - (flowLayout.sectionInset.left + flowLayout.sectionInset.right)); 
     return CGSizeMake(cellWidth, cellWidth/2); 
    } else { 
     float cellWidth = (CGRectGetWidth(collectionView.frame) - (flowLayout.sectionInset.left + flowLayout.sectionInset.right) - flowLayout.minimumInteritemSpacing)/2; 
     return CGSizeMake(cellWidth, cellWidth); 
    } 
} 
1

セットセルの幅:

cell.frame = CGRectMake(x, y, self.view.frame.size.width * 20/100, height); 

またはあなたはあなたがこれを試すことができますCollectionView代表

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 
    return CGSizeMake(self.view.frame.size.width*20/100, 192.f); 
} 
1

スイフト2.2

override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    var flowLayout = (collectionView.collectionViewLayout as! UICollectionViewFlowLayout) 
    if indexPath.item % 3 == 0 { 
     var cellWidth: Float = (CGRectGetWidth(collectionView.frame) - (flowLayout.sectionInset.left + flowLayout.sectionInset.right)) 
     return CGSizeMake(cellWidth, cellWidth/2) 
    } 
    else { 
     var cellWidth: Float = (CGRectGetWidth(collectionView.frame) - (flowLayout.sectionInset.left + flowLayout.sectionInset.right) - flowLayout.minimumInteritemSpacing)/2 
     return CGSizeMake(cellWidth, cellWidth) 
    } 
} 

スイフト3.0

override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    var flowLayout: UICollectionViewFlowLayout? = (collectionView.collectionViewLayout as? UICollectionViewFlowLayout) 
    if indexPath.item % 3 == 0 { 
     var cellWidth: Float? = (collectionView.frame.width - (flowLayout?.sectionInset?.left + flowLayout?.sectionInset?.right)) 
     return CGSize(width: CGFloat(cellWidth), height: CGFloat(cellWidth/2)) 
    } 
    else { 
     var cellWidth: Float? = (collectionView.frame.width - (flowLayout?.sectionInset?.left + flowLayout?.sectionInset?.right) - flowLayout?.minimumInteritemSpacing)/2 
     return CGSize(width: CGFloat(cellWidth), height: CGFloat(cellWidth)) 
    } 
} 
2

これは最も単純な解決策です。

UIViewControllerは、UICollectionViewDelegateFlowLayoutを実装する必要があります。次に、以下の関数をUIviewControllerに実装する必要があります。

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

    let width = heroCollectionView.frame.size.width/2; 


    return CGSize(width: width, height: 230) 

} 

さらに、各セルのスペーシング値をストーリーボードで調整できます。コレクションビューをクリックし、サイズインスペクタを表示します。

関連する問題