iPhone X 横屏时,需要检查 safeAreaInsets,以便在左右两侧留出适当的间距。UITableView 有一个新的 insetsContentViewsToSafeArea
属性(默认为 true),可以自动将单元格内容保留在安全区域。
我很惊讶 UICollectionView 似乎没有类似的东西。我期望对于垂直滚动的集合视图,在横向时左右两侧会被插入到安全区域中(反之,在纵向时如果需要,水平滚动的集合视图将被插入到安全区域)。
确保此行为最简单的方法似乎是添加到集合视图控制器中:
- (void)viewSafeAreaInsetsDidChange {
[super viewSafeAreaInsetsDidChange];
UIEdgeInsets contentInset = self.collectionView.contentInset;
contentInset.left = self.view.safeAreaInsets.left;
contentInset.right = self.view.safeAreaInsets.right;
self.collectionView.contentInset = contentInset;
}
假设contentInset.left/right通常为零。
(注:对于UICollectionViewController,需要使用self.view.safeAreaInsets
;在调用此代码时,对safeAreaInsets
的更改尚未奇怪地传播到self.collectionView
)。
我错过了什么吗?那个样板文件足够简单,但现在它实际上对于每个接触屏幕边缘的集合视图都是必需的。看起来很奇怪,苹果没有提供默认启用此功能的东西。
mycustomheaderview.layoutMarginsGuide.leadingAnchor
。这有点棘手,以前从未在代码中做过这样的事情。 - Jonny