2016-11-07 79 views
0

我在UIView中有CollectionView。如何將CollectionViewCell調整爲屏幕寬度的50%?我想在CollectionView中創建2列。在UIView中調整CollectionViewCell的大小

class Class_MyUIViewClass: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate 
{ 

此外,我在CollectionViewCell中有圖像和標籤。

回答

0

可以使用UICollectionView的sizeForItemAt方法來管理其單元尺寸

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    let screenWidth = UIScreen.main.fixedCoordinateSpace.bounds.width 
    return CGSize(width: screenWidth/2, height: yourHeight) 
} 

又確認到UICollectionViewDelegateFlowLayout

+0

myfile.swift:49:10:實例方法 ':willDisplaySupplementaryView:forElementKind:的CollectionView(_在:)' '的CollectionView(的CollectionView:佈局sizeForItemAtIndexPath :)' 幾乎可選要求匹配協議的 'UICollectionViewDelegate' –

+0

「CGSizeMake '在Swift中不可用 –

+0

查看更新回答 – Rajat

0

假設你知道所需的細胞高度,順應UICollectionViewDelegateFlowLayout

class Class_MyUIViewClass: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

and implement sizeForItemAtIndexPath

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    let desirednumberOfCollumns: CGFloat = 2 
    let width = collectionView.frame.width/desirednumberOfCollumns 
    return CGSize(width, yourHeight) 
} 
+0

'CGSizeMake'在Swift中不可用:) –

+0

@ Vladimir Zhelnov:對,謝謝! – shallowThought