我试图使用estimatedItemSize动态调整collectionView单元的大小以适应它们是内容塔--我遇到了一些不同设备大小的问题,特别是宽度没有调整大小,它只是保持了与界面构建器相同的大小。
我在iPhone 7上做了界面生成器的工作,所以屏幕宽度是375。在我的storyBoard中,我有以下内容。
在这个牢房里我有:
在我的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
IPhone7I构建它-利润率正常: 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的顶部角进行文件处理。但它也填充了细胞,因为它每一行塞得更多。
屏幕较小,单元格也有类似的问题:
发布于 2017-02-16 11:19:07
解决方案是实现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
不会成功
https://stackoverflow.com/questions/42264165
复制相似问题