iOS 适配iPhone X的方法
因为iPhoneX奇特的刘海存在,iOS11之后系统深化了“安全区域”概念,安全区域就是从屏幕上切除最大的矩形之外的区域。
iOS11后UIScrollView新增contentInsetAdjustmentBehavior属性,默认配置UIScrollViewContentInsetAdjustmentAutomatic,效果上就是没使用安全区域。若针对具体页面需要使用安全区域,可以查看API中新增加的那些属性。
/**
*适配iPhoneX的安全区域
*isUse=1表示使用安全区域
*isUse=0表示不使用安全区域
*/
+(void)adaptationSafeAreaWith:(UIScrollView*)svuseArea:(NSInteger)isUse{
if([[svclass]isSubclassOfClass:[UIWebViewclass]]){
UIWebView*webView=(UIWebView*)sv;
for(UIView*aViewin[webViewsubviews]){
if([aViewisKindOfClass:[UIScrollViewclass]]){
sv=(UIScrollView*)aView;
break;
}
}
}
#ifdef__IPHONE_11_0
if([svrespondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]){
if(isUse){
if(@available(iOS11.0,*)){
sv.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;
if([[svclass]isSubclassOfClass:[UITableViewclass]]){
UITableView*tv=(UITableView*)sv;
[tvsetInsetsContentViewsToSafeArea:NO];
}
}else{
//Fallbackonearlierversions
}
}else{
if(@available(iOS11.0,*)){
sv.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentAlways;
}else{
//Fallbackonearlierversions
}
}
}
#endif
}
typedefNS_ENUM(NSInteger,UIScrollViewContentInsetAdjustmentBehavior){
UIScrollViewContentInsetAdjustmentAutomatic,//Similarto.scrollableAxes,butforbackwardcompatibilitywillalsoadjustthetop&bottomcontentInsetwhenthescrollviewisownedbyaviewcontrollerwithautomaticallyAdjustsScrollViewInsets=YESinsideanavigationcontroller,regardlessofwhetherthescrollviewisscrollable
UIScrollViewContentInsetAdjustmentScrollableAxes,//Edgesforscrollableaxesareadjusted(i.e.,contentSize.width/height>frame.size.width/heightoralwaysBounceHorizontal/Vertical=YES)
UIScrollViewContentInsetAdjustmentNever,//contentInsetisnotadjusted
UIScrollViewContentInsetAdjustmentAlways,//contentInsetisalwaysadjustedbythescrollview'ssafeAreaInsets
}API_AVAILABLE(ios(11.0),tvos(11.0));
以上这篇iOS适配iPhoneX的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短