這篇文章將為大家詳細講解有關iOS如何適配iPhone X,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
因為iPhone X奇特的劉海存在,iOS11之后系統深化了“安全區域”概念,安全區域就是從屏幕上切除最大的矩形之外的區域。
iOS11后UIScrollView新增contentInsetAdjustmentBehavior屬性,默認配置UIScrollViewContentInsetAdjustmentAutomatic,效果上就是沒使用安全區域。若針對具體頁面需要使用安全區域,可以查看API中新增加的那些屬性。
/**
* 適配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
}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”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。