UICollectionViewLayout 不适用于 Swift 5 和 Xcode 11 中的 UIImage

Yog*_*tel 0 uiimageview uiimage uicollectionview uicollectionviewlayout swift

请检查此附加代码并缩短屏幕。当我设置容器视图的颜色时它工作正常,但是当我在单元格中添加 UIImage 时它不起作用。我更改了 imageview 内容模式,但它不起作用。

class ViewController: UIViewController {

@IBOutlet weak var segColumn: UISegmentedControl!
@IBOutlet weak var collectionView: UICollectionView!

fileprivate var items: [UIImage] = [ // My images ]

override func viewDidLoad() {
    super.viewDidLoad()
    self.setup()
}
}

extension ViewController: UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
    cell.imgCell.image = items[indexPath.row]
    cell.imgCell.contentMode = .scaleAspectFill
    cell.contentView.backgroundColor = .red
    return cell
}
}

extension ViewController: UICollectionViewDelegateFlowLayout{

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (collectionView.bounds.width/3 - 2), height: (collectionView.bounds.width/3))
    }


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 2
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 2
    }
}
Run Code Online (Sandbox Code Playgroud)

不适用于 UIImage

删除 UIImage 时工作

Yog*_*tel 12

请尝试此解决方案。我认为 Xcode 11 有一些变化。将 CollectionView Estimate Size 更改为 None 然后它将起作用。

请选择您的故事板文件