UIScrollView has Difference Default contentInsets on iOS 11

Our iOS team’s tech lead, Plasma Chen, shared his experience about UIScrollView’s difference on iOS 11.

When po NSStringFromUIEdgeInsets(self.scrollView.contentInset) in UIScrollView’s scrollViewDidScroll (which didn’t set any contentInsets), you will get different result on iOS 10 and 11.

On iOS 10, you will get {64, 0, 49, 0} as expected.

On iOS 11, now you will get {0, 0, 0, 0}; it didn’t adjust automatically, even when we turn on adautomaticallyAdjustsScrollViewInsets.

And if we get safeAreaInsets by po NSStringFromUIEdgeInsets((UIEdgeInsets)[self.scrollView safeAreaInsets]), you will get {76, 0, 49, 0} and contentOffset: (0, -76)


So how to get correct contentInset in iOS 11, even when we had adjusted contentInset by code? we could use adjustedContentInset, which is new property of UIScrollView:

Use this property to obtain the adjusted area in which to draw content. The contentInsetAdjustmentBehavior property determines whether the safe area insets are included in the adjustment. The safe area insets are then added to the values in the contentInset property to obtain the final value of this property.

It would refer contentInsetAdjustmentBehavior to return contentInset, whenever we had adjust contentInset or safe area insets on iOS 11. We will get {76, 0, 49, 0} when we execute
po NSStringFromUIEdgeInsets((UIEdgeInsets)[self.scrollView adjustedContentInset]), and that’s what we can want to get in iOS 11.


In the sharing, our iOS tech leader also suggested us to set UIScrollViewContentInsetAdjustmentNever before setting UICollectionView’s contentInset. As following,

if (@available(iOS 11, *)) {
self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
self.collectionView.contentInset = UIEdgeInsetsMake(…);