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 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语