我正在尝试使用集合视图创建应用程序。无论我做什么,都无法更改其大小。在视图控制器中,它看起来比程序为larger时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的出口。
答案 0 :(得分:2)
要调用sizeForItemAt方法,请确保您的类同时采用 UICollectionViewDelegate 和 UICollectionViewDelegateFlowLayout 协议。
答案 1 :(得分:1)
您必须在viewController中使用此功能
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: UIScreen.main.bounds.height, height: <Desired Height>)
}
}