作者:陈政德169384 | 来源:互联网 | 2023-05-17 18:48
WheniPhoneXisusedlandscape,youresupposedtochecksafeAreaInsetstomakesuitablylargegutt
When iPhone X is used landscape, you're supposed to check safeAreaInsets to make suitably large gutters on the left and right. UITableView has the new insetsContentViewsToSafeArea
property (default true) to automatically keep cell contents in the safe area.
当iPhone X被使用的时候,你应该检查safeAreaInsets,在左边和右边做出适当的大排水口。UITableView具有新的insetsContentViewsToSafeArea属性(默认为true),可以在安全区域自动保存单元格内容。
I'm surprised that UICollectionView seems to not have anything similar. I'd expect that for a vertically-scrolling collection view, the left and right sides would be inset to the safe area when in landscape (and conversely, a horizontally-scrolling collection view would be inset if needed in portrait).
我很惊讶UICollectionView似乎没有类似的东西。我希望在垂直滚动的集合视图中,在风景中,左边和右边会被插入到安全区域(反之,如果需要的话,一个水平滚动的集合视图会被插入)。
The simplest way to ensure this behaviour seems to be to add to the collection view controller:
确保这种行为的最简单方法似乎是添加到集合视图控制器:
- (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;
}
... assuming contentInset.left/right are normally zero.
…假设contentInset。左/右通常是零。
(NOTE: yes, for a UICollectionViewController, that needs to be self.view.safeAreaInsets
; at the time this is called, the change to safeAreaInsets
has oddly not yet propagated to self.collectionView
)
(注:是的,对于UICollectionViewController,需要self。view. safeareainsets;在这个调用的时候,对safeAreaInsets的更改还没有传播到self。collectionview)
Am I missing something? That boilerplate is simple enough, but it's effectively necessary now for every collection view that touches a screen edge. It seems really odd that Apple didn't provide something to enable this by default.
我遗漏了什么东西?这个样板文件很简单,但是现在对于每一个触摸屏幕边缘的集合视图来说都是必要的。苹果在默认情况下没有提供支持这一功能,这似乎真的很奇怪。
5 个解决方案
31
While Nathan is correct about the versatility of UICollectionView with various layouts, I was mainly concerned about the "default" case where one is using UICollectionViewFlowLayout.
虽然内森对UICollectionView的多种布局的多样性是正确的,但我主要关心的是使用UICollectionViewFlowLayout的“默认”情况。
Turns out, iOS 11 has added a sectionInsetReference
property to UICollectionViewFlowLayout
. The official documentation on it currently lacks a description, however the headers describe it as
事实证明,ios11在UICollectionViewFlowLayout中添加了sectionInsetReference属性。官方文件目前没有描述,但是标题描述的是。
The reference boundary that the section insets will be defined as relative to. Defaults to .fromContentInset
.
section insets的引用边界将被定义为相对的。默认为.fromContentInset。
NOTE: Content inset will always be respected at a minimum. For example, if the sectionInsetReference equals .fromSafeArea
, but the adjusted content inset is greater that the combination of the safe area and section insets, then section content will be aligned with the content inset instead.
注:内容嵌入将永远被尊重。例如,如果sectionInsetReference等于。fromsafearea,但是调整后的内容inset更大,即安全区域和section insets的组合,那么section内容将与内容inset对齐。
The possible values are
可能的值是
@available(iOS 11.0, *)
public enum UICollectionViewFlowLayoutSectionInsetReference : Int {
case fromContentInset
case fromSafeArea
case fromLayoutMargins
}
and setting it to .fromSafeArea
produces the desired results, i.e., when initially in portrait orientation:
并将其设置为。fromsafearea产生所需的结果,即。,最初在肖像方向:
then when rotating to landscape, the cells are inset such that they are entirely within the safe area:
然后当旋转到景观时,这些细胞被镶嵌在一起,它们完全在安全区域内:
... HOWEVER, there's currently a bug, and when rotating back to portrait after the view has been in landscape, it continues to act as if the left/right safeAreaInsets are set to the landscape values:
…然而,目前有一个bug,当视图在风景中出现后,当它旋转回到肖像时,它会继续表现为左/右的safeAreaInsets设置为景观值:
I've filed a radar (rdar://34491993) regarding this issue.
关于这个问题,我已经提交了雷达(rdar://34491993)。