Because of the unique bangs of iPhone X, the system has deepened the concept of "safe area" after iOS11. The safe area is to cut off the area outside the largest rectangle from the screen.
After iOS11, UIScrollView has added contentInsetAdjustmentBehavior attribute, and UIScrollViewContentInsetAdjustmentAutomatic is configured by default. In effect, the security area is not used. If you need to use a secure area for specific pages, you can view the newly added properties in the API.
/** * A safe area that is suitable for iPhone X * isUse = 1 means using a safe area * isUse = 0 means that the safe area is not used */ + (void)adaptationSafeAreaWith:(UIScrollView *)sv useArea:(NSInteger)isUse { if ([[sv class] isSubclassOfClass:[UIWebView class]]) { UIWebView *webView = (UIWebView *)sv; for (UIView *aView in [webView subviews]) { if ([aView isKindOfClass:[UIScrollView class]]) { sv = (UIScrollView *)aView; break; } } } #ifdef __IPHONE_11_0 if ([sv respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) { if (isUse) { if (@available(iOS 11.0, *)) { = UIScrollViewContentInsetAdjustmentNever; if ([[sv class] isSubclassOfClass:[UITableView class]]) { UITableView *tv = (UITableView *)sv; [tv setInsetsContentViewsToSafeArea:NO]; } } else { // Fallback on earlier versions } } else { if (@available(iOS 11.0, *)) { = UIScrollViewContentInsetAdjustmentAlways; } else { // Fallback on earlier versions } } } #endif }
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) { UIScrollViewContentInsetAdjustmentAutomatic, // Similar to .scrollableAxes, but for backward compatibility will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewInsets = YES inside a navigation controller, regardless of whether the scroll view is scrollable UIScrollViewContentInsetAdjustmentScrollableAxes, // Edges for scrollable axes are adjusted (., /height > /height or alwaysBounceHorizontal/Vertical = YES) UIScrollViewContentInsetAdjustmentNever, // contentInset is not adjusted UIScrollViewContentInsetAdjustmentAlways, // contentInset is always adjusted by the scroll view's safeAreaInsets } API_AVAILABLE(ios(11.0),tvos(11.0));
The above method to adapt to iPhone X for iOS is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.