根据各种屏幕尺寸自动布局集合视图 [英] Auto Layout Collection view according to various screen sizes
本文介绍了根据各种屏幕尺寸自动布局集合视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在普通视图控制器中使用集合视图,使用集合视图委托和数据源.我正在使用 sizeForItemAtIndexPath 但它不会调整单元格的大小.
I'm using a collection view inside a normal view controller using the collection view delegate and data source. I'm using the sizeForItemAtIndexPath but it doesn't resize the cell.
let screenSize: CGRect = UIScreen.main.bounds
func collectionView(_collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSize(width: screenSize.width / 3, height: screenSize.width / 3)
}
正确使用 autoLayout 为集合视图分配了约束.
The collection view is assigned the constraints using autoLayout properly.
它在 iPhone5 中的样子
它应该是什么样子
我知道使用了estimatedItemSize,但我无法使用它,因为这不是UICollectionViewController.感谢您的任何建议?
I know the estimatedItemSize is used but I'm unable to use it since this is not a UICollectionViewController. Thanks for any suggestion?
推荐答案
请确保:
- 确认
UICollectionViewDelegateFlowLayout
. - collectionView 的宽度 = 屏幕的宽度.
- 单元格的最小间距为零.您可以从 Interface Builder(选择 collectionView -> 显示大小检查器 -> 将最小间距设置为 0)或通过实现
minimumInteritemSpacingForSectionAt
返回零来更改它.
- Confirming to
UICollectionViewDelegateFlowLayout
. - collectionView's width = screen's width.
- Min Spacing for cells is zero. You can change it from the Interface Builder (Select the collectionView -> show size inspector -> set min spacing to 0), or by implementing
minimumInteritemSpacingForSectionAt
returning zero.
我希望回答了您的问题.
I hope that answered your question.
这篇关于根据各种屏幕尺寸自动布局集合视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文