2016-10-09 48 views
1

我在使用集合视图委托和数据源的普通视图控制器内使用集合视图。我正在使用sizeForItemAtIndexPath,但它不会调整单元格的大小。根据各种屏幕尺寸的自动布局集合视图

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为集合视图分配约束。

它的外观在iPhone5的 enter image description here

应该如何看待 enter image description here

我知道estimatedItemSize使用,但我无法使用它,因为这不是一个UICollectionViewController。感谢您的任何建议?

回答

1

确保:

  • 确认到UICollectionViewDelegateFlowLayout
  • collectionView的宽度=屏幕的宽度。
  • 最小单元格间距为零。你可以从界面生成器中改变它(选择collectionView - >显示大小检查器 - >将最小间距设置为0),或者通过执行minimumInteritemSpacingForSectionAt返回零。

我希望能回答你的问题。