swift - 更改 collectionView 单元格的大小
问题描述
我的数学不太好,所以我想把这个垂直矩形改成水平的,就像下面的小矩形一样,在一行中,我认为约束有问题
let areaView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.register(customCell.self, forCellWithReuseIdentifier: "cell")
cv.backgroundColor = UIColor(red: 245, green: 245, blue: 245, a: 1)
return cv
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 245, green: 245, blue: 245, a: 1)
view.addSubview(areaView)
areaView.delegate = self
areaView.dataSource = self
setUpLayout()
}
func setUpLayout(){
areaView.topAnchor.constraint(equalTo: view.topAnchor, constant: 40).isActive = true
areaView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
areaView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
areaView.heightAnchor.constraint(equalTo: areaView.widthAnchor, multiplier: 0.5).isActive = true
}
}
extension Search: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width/2.5, height: collectionView.frame.width/2)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! customCell
cell.data = self.data[indexPath.row]
cell.backgroundColor = UIColor.white
return cell
}
}
像这样
如果我将高度例如更改为 50,它将是这样的
return CGSize(width: collectionView.frame.width/2.5, height: 50)
解决方案
您似乎提供了错误height
的内容sizeForItem
,请尝试提供:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width/2.5, height: collectionView.frame.height/2)
}
另外,请查看:更改sizeForItem
方法以将大小调整为您需要的大小,如下所示:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width/2.5, height: 50)
}
这里我给它的高度是 50,你可以给任何你喜欢的高度。
您也可以通过调整 heightAnchor 来做到这一点:
// areaView heightAnchor like this:-
areaView.heightAnchor.constraint(equalToConstant: 50).isActive = true
// and set sizeForItem to this :-
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width/2.5, height: collectionView.frame.height)
}
推荐阅读
- android - 如何将捆绑包从 baseAdapter 发送到片段?
- php - 如何从模型中打印 Yii2 GridView 中的第一个提醒,第二个提醒?
- java - 在 docker 上运行应用程序期间连接被拒绝 - Spring Boot 和 mssql 服务器
- python - 在 python 中比较两个文件的最佳方法,为什么?
- javascript - 为什么这个 json 检索代码不起作用?
- python - 写入 GML 文件时出现 NetworkX 密钥错误
- android - Android单元测试中的资源文件访问“InvocationTargetException”
- python - 考虑顺序对 pandas 列执行累积计数
- html - Firefox 忽略最小宽度和最大宽度的图像尺寸属性
- c++ - 基本形状识别(openCV C++)