UICollectionView in landscape on iPhone X

前端 未结 6 647
余生分开走
余生分开走 2021-01-31 02:41

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 insetsContentViewsT

6条回答
  •  梦谈多话
    2021-01-31 02:48

    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.

    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

    The reference boundary that the section insets will be defined as relative to. Defaults to .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.

    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:

    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:

    I've filed a radar (rdar://34491993) regarding this issue.

提交回复
热议问题