![](/img/trans.png)
[英]How to make dynamic height for UICollectionViewCell with dynamic type and fixed width?
[英]how to make dynamic width of the UICollectionViewCell
我想動態設置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)
}
你可以嘗試這個......
- (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);
}
}
這是最簡單的解決方案。
你的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,然后查看大小檢查器。
將單元格的寬度設置為容器視圖的百分比,如:
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);
}
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))
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.