iOS 適配iPhone X的方法
因?yàn)閕Phone X奇特的劉海存在,iOS11之后系統(tǒng)深化了“安全區(qū)域”概念,安全區(qū)域就是從屏幕上切除最大的矩形之外的區(qū)域。
iOS11后UIScrollView新增contentInsetAdjustmentBehavior屬性,默認(rèn)配置UIScrollViewContentInsetAdjustmentAutomatic,效果上就是沒使用安全區(qū)域。若針對具體頁面需要使用安全區(qū)域,可以查看API中新增加的那些屬性。
/**
* 適配iPhone X的安全區(qū)域
* isUse = 1 表示使用安全區(qū)域
* isUse = 0 表示不使用安全區(qū)域
*/
+ (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的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IOS使用NSUserDefault去實(shí)現(xiàn)界面?zhèn)髦岛蛿?shù)據(jù)存儲(chǔ)
這篇文章主要介紹了IOS使用NSUserDefault去實(shí)現(xiàn)界面?zhèn)髦岛蛿?shù)據(jù)存儲(chǔ)的相關(guān)資料,需要的朋友可以參考下2017-07-07
ios之UIScrollerView滾動(dòng)視圖總結(jié)
本篇文章主要介紹了ios之UIScrollerView滾動(dòng)視圖總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
iOS中如何使用iconfont圖標(biāo)實(shí)例詳解
iconfont大家在開發(fā)中應(yīng)該會(huì)經(jīng)常用到,下面這篇文章主要給大家介紹了在iOS中如何使用iconfont圖標(biāo)實(shí)例的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07

