In my app, I present a view controller modally, and this view controller contains a UICollectionView
that fills the superview. Each cell in the collection view needs to be full-screen - the same size as the view's bounds. To accomplish this, I simply set the itemSize
of the UICollectionViewFlowLayout
to be the view's bound's size in viewDidLayoutSubviews
.
This works really well on all devices and orientations, except iPhone X in landscape. When presenting that screen in that case, it seems the safe area layout margins are coming into play and affecting positioning of the cell. The cell is pushed over from the left edge of the display, and pushed up such that the bottom of the cell is aligned to the vertical center of the home indicator. Inspecting the layout with View Debugger reveals the collection view fills the screen, but the cell's Y position is -10.67 and the X position is 0, even though it is inset from the leading of the collection view. Notably, this is logged in the console:
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>.
What's going on here and how can this be resolved?
By default, iOS 11 automatically adjusts content insets for iPhone X in landscape, causing this issue to occur. To resolve it, you can simply disable the automatic content inset adjustment, as it's not needed nor desired in this scenario.
collectionView.contentInsetAdjustmentBehavior = .never
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With