本文介绍了以编程方式将UICollectionViewCell宽度设置为UICollectionView宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在集合视图中,我们如何根据整个集合视图设置宽度和高度的像元大小。它将设置小到大的设备(例如:如果集合视图的总宽度= 375.0000,我们将其分为3.因此它将为所有大型和小型设备设置。)
In the collection view how do we set the cell size of width and height based on the overall collection view. it will set for small to large devices (for example : if the collection view overall width = 375.0000 and we divide into 3. so it will set for the all the large and small devices).
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"SETTING SIZE FOR ITEM AT INDEX %d", (int)indexPath.row);
CGSize mElementSize = CGSizeMake(125, 125);
return mElementSize;
}
推荐答案
在代码中设置UICollectionViewDelegateFlowLayout方法
Set UICollectionViewDelegateFlowLayout method in your code
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width:(collectionView.bounds.size.width/numberOfCellInRow), height:270)
}
并且还在单元格的awakeFromNib()方法中添加这两行,以根据单元格大小更新单元格的约束。
And also add this two line in your cell's awakeFromNib() method for update your cell's constraint according to cell size.
override func awakeFromNib()
{
self.contentView.autoresizingMask.insert(.flexibleHeight)
self.contentView.autoresizingMask.insert(.flexibleWidth)
}
这篇关于以编程方式将UICollectionViewCell宽度设置为UICollectionView宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!