【发布时间】:2017-09-29 04:34:22
【问题描述】:
在我的应用程序中,我以模态方式展示了一个视图控制器,这个视图控制器包含一个填充超级视图的UICollectionView。集合视图中的每个单元格都需要全屏显示 - 与视图边界的大小相同。为此,我只需将UICollectionViewFlowLayout 的itemSize 设置为viewDidLayoutSubviews 中视图的边界大小。
这在所有设备和方向上都非常有效,横向的 iPhone X 除外。在这种情况下呈现该屏幕时,似乎安全区域布局边距正在发挥作用并影响单元格的定位。单元格从显示器的左边缘被推过,然后向上推,以使单元格的底部与主指示器的垂直中心对齐。使用 View Debugger 检查布局显示集合视图填满了屏幕,但单元格的 Y 位置为 -10.67,X 位置为 0,即使它从集合视图的前导插入。值得注意的是,这是在控制台中记录的:
The behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fa6311253b0>, and it is attached to <UICollectionView: 0x7fa63207e800; frame = (0 0; 812 375); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x608000244d70>; layer = <CALayer: 0x60800023ef00>; contentOffset: {-44, 0}; contentSize: {0, 812}; adjustedContentInset: {0, 44, 21, 44}> collection view layout: <UICollectionViewFlowLayout: 0x7fa6311253b0>.
这是怎么回事?如何解决?
【问题讨论】:
-
只是好奇,为什么不在
collectionView(_:layout:sizeForItemAt:)中设置itemSize 而不是viewDidLayoutSubviews? -
另外,如果使用第一个提到的设置 itemSize,请确保使用
view.frame作为宽度和高度,而不是view.bounds。
标签: ios uicollectionview ios11 iphone-x