UICollectionViewLayout在Swift 5和Xcode 11中不使用UIImage
请检查此附加代码和屏幕短路。当我设置容器视图的颜色时,它工作正常,但当我在单元格中添加UIImage时,它就不工作了。我更改了imageview内容模式,但它不工作UICollectionViewLayout在Swift 5和Xcode 11中不使用UIImage,swift,uicollectionview,uiimageview,uiimage,uicollectionviewlayout,Swift,Uicollectionview,Uiimageview,Uiimage,Uicollectionviewlayout,请检查此附加代码和屏幕短路。当我设置容器视图的颜色时,它工作正常,但当我在单元格中添加UIImage时,它就不工作了。我更改了imageview内容模式,但它不工作 class ViewController: UIViewController { @IBOutlet weak var segColumn: UISegmentedControl! @IBOutlet weak var collectionView: UICollectionView! fileprivate var items
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
}
}
这可能是因为红色在图像上占主导地位
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
}
尝试:
请尝试这个解决方案。我认为Xcode 11有一些变化。 将CollectionView估计大小更改为“无”,则它将起作用
否如果我对imageview代码进行注释,则它会起作用。。我通过添加内容视图颜色来检查这一点。但是我添加了uIImageview,然后它就不工作了,明白了吗?我尝试了你的代码不工作。。任何其他的解决方案谢谢:)当你去掉颜色时会发生什么?它在屏幕上显示为UIImage Screen short。你能澄清你的意思吗?我也有类似的问题,我正在寻找几个小时来解决这个问题。这解决了我的问题,现在我可以睡觉了。谢谢,eeeee,非常感谢你
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
}