Im试图使用estimatedItemSize动态调整collectionView单元格的大小以适合它们的内容塔我在使用不同的设备尺寸时遇到了一些问题,特别是宽度未调整大小,它只是保持相同尺寸因为它在界面构建器中。CollectionView estimatedItemSize按预期工作
我已经在iPhone 7上完成了我的界面生成器的东西,所以屏幕宽度是375.在我的故事板中,我有以下内容。
- outerCollectionView钉在上海华的所有4个侧面与0余量的瓦特
- outerCollectionView细胞:339个H:550
该小区内我有:
- innerCollectionView钉扎到单元格的所有4边,具有0余量
- innerCollectionView常量w:339 h:550在IB中添加
在我的DataSource和委托类的我已经在viewDidLoad中函数定义我的流程布局:
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let flow = outerCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
flow.estimatedItemSize = CGSize(width: width - ((width/18.75)), height: height/4)
flow.minimumLineSpacing = (width/37.5)
flow.sectionInset = UIEdgeInsets(top: (width/37.5), left: (width/37.5), bottom: (width/37.5), right: (width/37.5))
和保持“内部”收集我设置一些这些常量的约束自定义单元格类中并定义流布局大小的物品:
// This is the correct screen size
// let width = innerCollectionView.frame.size.width
let width = UIScreen.main.bounds.size.width
let flow = innerCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
flow.sectionInset = UIEdgeInsetsMake((width/37.5), (width/37.5), (width/37.5), (width/37.5))
flow.itemSize = CGSize(width: (width/6 - (width/37.5)), height: (width/6 - (width/37.5)))
flow.minimumInteritemSpacing = (width/37.5)
flow.minimumLineSpacing = (width/37.5)
innerCollectionHeight.constant = innerCollectionView.collectionViewLayout.collectionViewContentSize.height
innerCollectionWidth.constant = innerCollectionView.collectionViewLayout.collectionViewContentSize.width
现在,这一切工作正常,细胞调整到垂直很好地适应在iPhone模拟器7我的内容,显示出正确填充一列单元格。但是,当我加大设备大小不调整单元格的宽度。而当我走到一个较小的设备时,我得到了一大堆关于单元格宽度如何变大的Xcode错误,因为它没有被调整大小,但没有显示。 (下面的一些照片)
2017-02-16 12:53:49.632 ParseStarterProject-Swift[26279:394906] The behavior of the UICollectionViewFlowLayout is not defined because:
2017-02-16 12:53:49.632 ParseStarterProject-Swift[26279:394906] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
2017-02-16 12:53:49.632 ParseStarterProject-Swift[26279:394906] Please check the values returned by the delegate.
2017-02-16 12:53:49.633 ParseStarterProject-Swift[26279:394906] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7be38920>, and it is attached to <UICollectionView: 0x7db03600; frame = (0 0; 320 504); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7d61a550>; layer = <CALayer: 0x7be95880>; contentOffset: {0, 0}; contentSize: {320, 20}> collection view layout: <UICollectionViewFlowLayout: 0x7be38920>.
所以,很显然我不能使用
flow.itemSize =
设置在外网孔的大小,因为我需要的估计的大小来调节的垂直长度和拥抱的内容。我只是无法弄清楚为什么宽度不会适合屏幕尺寸?
大尺寸的装置 - 余量太大:CV宽度339
iPhone 7我建立它 - 边距正常:CV宽度339
----编辑-----
试图覆写szeForItemAt但没有改变。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = self.view.frame.size.width
let height = self.view.frame.size.height
if collectionView == self.outerCollectionView {
return CGSize(width: width - 100, height: height/4)
} else {
return CGSize(width: (width/6 - (width/75)), height: (width/6 - (width/75)))
}
}
---- ---- EDIT
我可以编辑的优选项目抓住细胞。使用高度压缩的大小选项和设置您自己的尺寸为宽度,(宽度/ 18.7)为宽度减去左和右插图先前定义
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
let attributes = layoutAttributes.copy() as! UICollectionViewLayoutAttributes
let width = UIScreen.main.bounds.size.width
let desiredWidth = width - (width/18.75)
//let desiredWidth = systemLayoutSizeFitting(UILayoutFittingExpandedSize).width
let desiredHeight = systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
attributes.frame.size.width = desiredWidth
attributes.frame.size.height = desiredHeight
return attributes
}
卜此充塞了约束作为所述图像中示出的下方,我认为这可能只是一个脚本,我已经运行了倒UIViews的顶角。但它也填补了细胞,因为它增加了每条线。
更小的屏幕,类似的问题与细胞: