[英]IOS 11: UICollectionView on iPhone X is sized wrong
I have a collection view that the frame appears too tall on the iPhone X. On every other device, the sizing and scrolling works properly as shown below:我有一个集合视图,框架在 iPhone X 上显得太高。在其他所有设备上,大小和滚动都正常工作,如下所示:
However on iPhone X, it looks like this:但是在 iPhone X 上,它看起来像这样:
The top row is cut off, and it does not scroll all the way down to the last row.顶行被截断,它不会一直向下滚动到最后一行。 Somehow, the sizing correctly calculates the width but not the height, which is about 70 pixels too tall.
不知何故,调整大小正确计算了宽度而不是高度,大约 70 像素太高了。 (I'm not worried about the top and bottom bars. I'll fix those later)
(我不担心顶部和底部栏。稍后我会修复它们)
I'm guessing this has something to do with the inset adjustments for the iPhone X screen, but I can't figure out how to fix it.我猜这与 iPhone X 屏幕的插入调整有关,但我不知道如何修复它。 I've tried this in where I size the collection view:
我已经在我调整集合视图大小的地方尝试过这个:
if #available(iOS 11.0, *) {
collectionView.contentInsetAdjustmentBehavior = .always
}
However, I can't seem to fix it.但是,我似乎无法修复它。
Edit:编辑:
To clarify, the set up for this menu is as follows, there are actually two collection views onscreen.澄清一下,这个菜单的设置如下,屏幕上实际上有两个集合视图。 The first scrolls horizontally with paging enabled so that it locks onto each cell.
第一个在启用分页的情况下水平滚动,以便锁定到每个单元格。 The other collection views are the cells for the first one, and they scroll vertically.
其他集合视图是第一个的单元格,它们垂直滚动。 We'll call these subCollectionViews.
我们将调用这些 subCollectionViews。
The subCollectionViews are receiving a size from the original collection view thats too tall. subCollectionViews 正在接收来自太高的原始集合视图的大小。 On the storyboard, the collection view's height is defined with respect to the top bar and the bottom paging bar as flush.
在故事板上,集合视图的高度相对于顶部栏和底部分页栏定义为齐平。 In the story board, the height of the collection view is about 70 pixels larger than the calculated height during runtime.
在故事板中,集合视图的高度比运行时计算的高度大约 70 个像素。
So for the cell's layout guide:所以对于单元格的布局指南:
override func awakeFromNib() {
cellImage = UIImageView(frame: contentView.frame)
cellImage?.contentMode = .scaleAspectFill
cellImage?.clipsToBounds = true
contentView.addSubview(cellImage!)
}
and for the collection view's layout:对于集合视图的布局:
layout.scrollDirection = .vertical
layout.minimumLineSpacing = 8
layout.minimumInteritemSpacing = 8
for the storyboard presets:对于故事板预设:
I think this is what y'all asked for.我想这就是你们所要求的。 If there's anything you need to see, just ask.
如果有什么需要看的,尽管问。
I also facing this issue.我也面临这个问题。 Need to called safeAreaLayoutGuide for control the size of item.
需要调用safeAreaLayoutGuide来控制item的大小。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if #available(iOS 11.0, *) {
return view.safeAreaLayoutGuide.layoutFrame.size
} else {
// Fallback on earlier versions
return view.frame.size
}
}
Add below lines of code.添加以下代码行。 I think it will solve your problem.
我认为它会解决你的问题。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.aCollectionVew.setNeedsLayout()
self.aCollectionVew.layoutIfNeeded()
}
Please take all traits, spacing
and contentInsetAdjustmentBehavior
.请获取所有特征、
spacing
和contentInsetAdjustmentBehavior
。
Happy Coding...:)快乐编码...:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.