因為iPhone X奇特的劉海存在,iOS11之后系統深化了“安全區域”概念,安全區域就是從屏幕上切除最大的矩形之外的區域。
iOS11后UIScrollView新增contentInsetAdjustmentBehavior屬性,默認配置UIScrollViewContentInsetAdjustmentAutomatic,效果上就是沒使用安全區域。若針對具體頁面需要使用安全區域,可以查看API中新增加的那些屬性。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
/** * 適配iPhone X的安全區域 * isUse = 1 表示使用安全區域 * isUse = 0 表示不使用安全區域 */ + (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, *)) { sv.contentInsetAdjustmentBehavior = 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, *)) { sv.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways; } else { // Fallback on earlier versions } } } #endif } |
1
2
3
4
5
6
|
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 (i.e., contentSize.width/height > frame.size.width/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)); |
以上這篇iOS 適配iPhone X的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。