我有 gicen collectionview 高度为 300 并添加了带有 imageview 和标签的单元格 对于给定约束的标签
bottom, leading, trailing = 10
对于给定顶部、前导、尾随 = 10、底部 = 40 的图像
代码:有了这个我就可以连线了。我什至看不到标签和太宽的图像视图。 我需要 200 高度和屏幕总宽度。如何解决这个问题?如何给出适当的单元格大小来显示图像和标签。请指导我。
class CollPagingViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionviewCellSize: UICollectionView!
var cellArray = ["one", "two", "three", "four", "five"]
override func viewDidLoad() {
super.viewDidLoad()
collectionviewCellSize.delegate = self
collectionviewCellSize.dataSource = self
if let layout = collectionviewCellSize.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .horizontal
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = UIScreen.main.bounds.width
let height = 200.0
print("cell size:\n width: \(width) heigth: \(height)")
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cellArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollCell2", for: indexPath) as! CollCell2
cell.textLbl.text = cellArray[indexPath.item]
return cell
}
o/p:
宽度:414.0 高度:200.0
编辑:此代码获得正确的单元格大小,但我想使用
sizeForItemAt
方法。如何?为什么我无法使用 sizeForItemAt
获得正确的像元大小?
override func viewDidLoad() {
super.viewDidLoad()
collectionviewCellSize.delegate = self
collectionviewCellSize.dataSource = self
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.scrollDirection = .horizontal
let width = collectionviewCellSize.frame.width
let height = collectionviewCellSize.frame.height
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.itemSize = CGSize(width: width, height: height)
self.collectionviewCellSize.collectionViewLayout = layout
}
添加 UI 元素(例如
UICollectionView
)时,会应用许多“默认”设置。
在这种情况下,一个值得注意的设置是估计像元大小。如果我们将集合视图添加到视图控制器,默认值是Automatic——它告诉UIKit使用单元格的内容来计算大小......*即使我们已经实现了
sizeForItemAt
。
因此,在您的流程布局配置中添加一行:
if let layout = collectionviewCellSize.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .horizontal
// need this line
layout.estimatedItemSize = .zero
}
现在,您应该获得正确的单元格大小。
附带说明 - 你不想使用
UIScreen.main.bounds.width
!如果集合视图被限制在安全区域(这是应该的),或者如果用户在 iPad 上以多任务模式运行您的应用程序,UIScreen.main.bounds.width
不会为您提供您想要的内容。
相反,使用集合视图的框架:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// don't use screen!
//let width = UIScreen.main.bounds.width
// use collection view frame
let width = collectionView.frame.width
let height = 200.0
print("cell size:\n width: \(width) heigth: \(height)")
return CGSize(width: width, height: height)
}