我试图使用estimatedItemSize来动态调整collectionView单元格以适应它们的内容塔我遇到了一些不同设备尺寸的问题,特别是宽度没有调整大小,它只是保持与界面构建器相同的大小
我在iPhone 7上完成了Interface Builder的操作,因此屏幕宽度为375.在我的故事板上,我有以下内容。
在这个单元格里面我有:
在我的DataSource和Delegate类中,我在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)))
}
}
----编辑----
我可以编辑单元格的首选项目。使用压缩尺寸选项高度并设置我自己的宽度尺寸(宽度/ 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的顶角。但是它也填满了细胞,因为它每行都在填充更多。
屏幕较小,与细胞类似的问题:
答案 0 :(得分:0)
解决方案是实施UICollectionViewDelegateFlowLayout
并像这样调用sizeForItemAt
:
extension YourClass: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout:UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//return size needed
return CGSize(width: (width / 6 - (width / 37.5)), height: (width / 6 - (width / 37.5)))
}
在运行时,返回的大小将是给出项目大小的大小。
注意:在没有sizeForItemAt
的情况下实施UICollectionViewDelegateFlowLayout
将无法成功