UICollectionViewLayout Not working with UIImage in Swift 5 and Xcode 11

雨燕双飞 提交于 2020-04-07 08:21:11

问题


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


回答1:


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




回答2:


it is probably because the red is dominating over the image

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
}

try:

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
}


来源:https://stackoverflow.com/questions/59181037/uicollectionviewlayout-not-working-with-uiimage-in-swift-5-and-xcode-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!