UICollectionViewLayout 不适用于 Swift 5 和 Xcode 11 中的 UIImage [英] UICollectionViewLayout Not working with UIImage in Swift 5 and Xcode 11

查看:25
本文介绍了UICollectionViewLayout 不适用于 Swift 5 和 Xcode 11 中的 UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Please check this attached code and screen short. It's work fine when I set the color of container view but when I add UIImage in cell then it's not working. I changed the imageview content mode but it's not working.

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
    }
}

推荐答案

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

Please try this solution. I think there are some changes from Xcode 11. Change the CollectionView Estimate Size to None then it will work.

这篇关于UICollectionViewLayout 不适用于 Swift 5 和 Xcode 11 中的 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆