我有一个 UICollectionView,它的单元格覆盖了整个屏幕。我的问题是右边有一个小空间显示不同的单元格。我希望每个单元格都覆盖屏幕的整个宽度。
这是我尝试过的:
- 在 Storyboard 中将最小间距设置为 0
- 设置每个单元格以适合整个 View 框架
private func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { return CGSize(width: view.frame.width, height: view.frame.height) }
我还应该做些什么来解决这个问题吗?
最佳答案
正确的 Swift 3 实现应该是
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let itemWidth = collectionView.bounds.width
let itemHeight = collectionView.bounds.height
return CGSize(width: itemWidth, height: itemHeight)
}
这将使每个 Collection View 单元格填满整个 Collection View 。它不应该是私有(private)的。现在您需要做的就是确保您的 Collection View 横跨整个 View 。
还要确保扩展您的 ViewController 以包含 UICollectionViewDelegateFlowLayout 以使上述方法起作用,并将 Storyboard 中的所有部分插入更改为 0。
关于ios - UICollectionViewCell 没有填满 Swift 3 中的整个屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40812737/