我正在尝试使用集合视图创建应用程序。无论我做什么,都无法更改其大小。在视图控制器,它看起来更大比当程序是RAN(下面的照片)。图像视图的大小与单元格相同。我也尝试使用
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: screenWidth/3, height: screenWidth/3)
}
但是什么都没有改变。
这是视图控制器代码:
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
let array = ["1,", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! myCell
cell.myImageView.image = UIImage(named: array[indexPath.row] + ".jpg")
return cell
}
}
myCell只是一个UICollectionViewCell类,带有imageview的出口。
要调用sizeForItemAt方法,请确保您的类同时采用UICollectionViewDelegate和UICollectionViewDelegateFlowLayout协议。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句