CollectionView Cell在加载Swift时更改其宽度和高度

时间:2017-12-11 20:11:18

标签: ios swift uicollectionview

我有一个加载动态内容的集合视图。当我试图加载其中一个单元格时,我设置的高度和宽度不同。 图像视图的内容模式是AspectFit,绘图设置为剪切到边界。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "included", for: indexPath) as! IncludeCollectionViewCell

    let imagename = fabric[indexPath.row].fabricImage

    let url = NSURL(string: "http://www.junaidkk.com/images/fabric/thumb/"+imagename!)!
    print(url)

    cell.fabricImage.sd_setImage(with: url as URL!, placeholderImage: UIImage(named: "default_logo"), options: .transformAnimatedImage, progress: nil, completed: nil)

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: CGFloat((collectionView.frame.size.width / 2) - 5), height: CGFloat(160))
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 10
}

see the screenshot

1 个答案:

答案 0 :(得分:1)

如果您正在使用storyBoard,请尝试以下操作:

CONTENT MODE

Aspect Fit更改为Aspect Fill或尝试下拉列表中的其他选项。

我相信Aspect Fill会解决您的问题,就像它对我一样:)