我想动态设置UICollectionViewCell
的宽度。
我在swift中找到了一些代码,我想要Objective-C中的代码。
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)
}
答案 0 :(得分:4)
你可以试试这个......
- (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 :(得分:4)
这是最简单的解决方案。
您的UIViewController应该实现UICollectionViewDelegateFlowLayout。然后,需要在UIviewController上实现以下函数。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = self.collectionView.frame.size.width / 2;
return CGSize(width: width, height: 230)
}
此外,您可以调整故事板上每个单元格的间距值。单击collectionView,然后查看大小检查器。
答案 2 :(得分:1)
将单元格的宽度设置为容器视图的百分比,如:
cell.frame = CGRectMake(x, y, self.view.frame.size.width * 20 / 100, height);
或者您可以使用CollectionView Delegates
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(self.view.frame.size.width*20/100, 192.f);
}
答案 3 :(得分:0)
Swift 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)
}
}
Swift 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))
}
}
答案 4 :(得分:0)
如果收集视图单元格中有文本,则只需要引用场景中的某个标签而没有宽度限制,然后每次调整项目大小时,只需为其分配文本,然后调用
目标C:
NSString *text = [arrayOfItems objectAtIndex:indexPath.item];
_outletoflbl.text = text;
return CGSizeMake(_outletoflbl.intrinsicContentSize.width + 60, 40);
迅速:
let text = arrayOfItems[indexPath.item]
_outletoflbl.text = text
return CGRect(width:_outletoflbl.intrinsicContentSize.width + 60, height: 40)
键是_outletoflbl
,是为其分配文本的标签,以获取最合适的字体和从接口构建器分配的其他属性的宽度!
检查结果: