2016-11-07 62 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