我在UIView中有CollectionView。如何调整CollectionViewCell屏幕宽度的50% ?我想在CollectionView中创建2列。
class Class_MyUIViewClass: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate
{
我也有图像和标签在CollectionViewCell.
您可以使用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
确认
假设您知道所需的单元格高度,请遵循UICollectionViewDelegateFlowLayout
:
class Class_MyUIViewClass: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
并实现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)
}