【问题标题】:UICollectionViewLayout Not working with UIImage in Swift 5 and Xcode 11UICollectionViewLayout 不适用于 Swift 5 和 Xcode 11 中的 UIImage
【发布时间】:2020-03-29 13:14:57
【问题描述】:

请检查此附加代码和屏幕简短。当我设置容器视图的颜色时它工作正常,但是当我在单元格中添加 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
    }
}

【问题讨论】:

    标签: swift uicollectionview uiimageview uiimage uicollectionviewlayout


    【解决方案1】:

    请尝试此解决方案。我认为 Xcode 11 有一些变化。 将 CollectionView Estimate Size 更改为 None 即可。

    【讨论】:

    • 我遇到了同样的问题,我正在寻找几个小时来解决这个问题。这解决了我的问题,现在我可以去睡觉了。谢谢。
    【解决方案2】:

    这可能是因为红色在图像上占主导地位

    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
    }
    

    尝试:

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

    【讨论】:

    • 不,如果我评论 imageview 代码那么它可以工作.. 我通过添加内容视图颜色来检查。但是我添加了 uIImageview 然后它不起作用吗?
    • 我尝试您的代码不起作用.. 任何其他解决方案谢谢 :)
    • 取出颜色会怎样?
    • 它显示我就像在屏幕短 UIImage 屏幕短。
    • 你能澄清一下你的意思吗?
    猜你喜欢
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 2014-06-22
    • 2021-11-08
    • 1970-01-01
    • 2020-07-28
    • 2020-07-16
    • 2019-08-03
    相关资源
    最近更新 更多