UICollectionViewCell 未在 Swift 3 中填满整个屏幕
UICollectionViewCell not filling the entire screen in Swift 3
我有一个 UICollectionView,它的单元格覆盖了整个屏幕。我的问题是右侧有一个小的 space 显示不同的单元格。我希望每个单元格都覆盖屏幕的整个宽度。
这是我尝试过的方法:
- 将情节提要中的最小间距设置为 0
- 设置每个单元格以适应整个视图框架
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)
}
这将使每个集合视图单元格填满整个集合视图。它不应该是私人的。现在您需要做的就是确保您的集合视图横跨整个视图。
还要确保扩展您的 ViewController 以包含 UICollectionViewDelegateFlowLayout 以使上述方法起作用,并将情节提要中的所有部分插图更改为 0。
UICollectionViewDelegate、UICollectionViewDataSource、UICollectionViewDelegateFlowLayout
添加所有 3 个代表很重要。特别是UIDelegateFlowLayout.
只是为了改进投票最多的答案:您需要添加 UICollectionViewDelegateFlowLayout
一致性,但如果您使用 UICollectionViewController
则不需要这样做 - 它已经符合所有必要的协议.
此外,实现 sizeForItemAt
的更短方法:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return collectionView.bounds.size
}
我有一个 UICollectionView,它的单元格覆盖了整个屏幕。我的问题是右侧有一个小的 space 显示不同的单元格。我希望每个单元格都覆盖屏幕的整个宽度。
这是我尝试过的方法:
- 将情节提要中的最小间距设置为 0
- 设置每个单元格以适应整个视图框架
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)
}
这将使每个集合视图单元格填满整个集合视图。它不应该是私人的。现在您需要做的就是确保您的集合视图横跨整个视图。
还要确保扩展您的 ViewController 以包含 UICollectionViewDelegateFlowLayout 以使上述方法起作用,并将情节提要中的所有部分插图更改为 0。
UICollectionViewDelegate、UICollectionViewDataSource、UICollectionViewDelegateFlowLayout
添加所有 3 个代表很重要。特别是UIDelegateFlowLayout.
只是为了改进投票最多的答案:您需要添加 UICollectionViewDelegateFlowLayout
一致性,但如果您使用 UICollectionViewController
则不需要这样做 - 它已经符合所有必要的协议.
此外,实现 sizeForItemAt
的更短方法:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return collectionView.bounds.size
}