[英]Collection View Cell Will Not Change Size
我正在嘗試創建一個帶有集合視圖的應用程序。 無論我做什么,我都無法改變它的大小。 在視圖 controller 中,它看起來比程序運行時大(下圖)。 圖像視圖與單元格大小相同。 我也嘗試過使用
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 class 與 imageview 的插座。
要調用 sizeForItemAt 方法,請確保您的 class 采用UICollectionViewDelegate和UICollectionViewDelegateFlowLayout協議。
你必須在你的 viewController 中使用這個 function
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: UIScreen.main.bounds.height, height: <Desired Height>)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.