gpt4 book ai didi

ios - CollectionView水平滚动约束问题

转载 作者:行者123 更新时间:2023-11-30 11:17:30 25 4
gpt4 key购买 nike

我有一个水平滚动的collectionView,而所有其他tableView单元格仅垂直滚动。我在 Storyboard中添加了约束以调整大小并适合所有尺寸的屏幕。当我在 iPhone X 模拟器上运行该项目时,它看起来很完美。但是,一旦我尝试其他尺寸(例如 7 plus),它会导致 Collection View 单元格变小并显示下一个单元格。但是,表格 View 单元格保持正确的大小。我怎样才能防止这种情况发生?

Here is a photo of both simulators

最佳答案

实现collectionView(_:layout:sizeForItemAt:)方法UICollectionViewDelegateFlowLayout

并返回单元格的正确大小,例如:

return CGSize(width: UIScreen.main.bounds.width, height: 100)

示例:

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout {
@IBOutlet private weak var collectionView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.delegate = self
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 100)
}
}

关于ios - CollectionView水平滚动约束问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51600905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com