ios 11和iphone x的相关适配问题及解决方法
有关iOS11,最大的变化就是增加了一个安全区域(safeArea)的概念,iOS11适配的大部分问题都是由于它引起的。
在ios11中,tableView会莫名偏移,解决办法:
//解决iOS11tableview会出现漂移,预估高度都设为0 self.tableView.estimatedRowHeight=0; self.tableView.estimatedSectionHeaderHeight=0; self.tableView.estimatedSectionFooterHeight=0;
解决scrollView,默认位移了,解决办法:
-(void)setScrollViewContentInsetAdjustmentBehavior:(UIScrollView*)scrollView{ #if__IPHONE_OS_VERSION_MAX_ALLOWED>=110000 if(@available(iOS11.0,*)){ if([scrollViewrespondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]){ scrollView.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever; } } #endif }
关于iphonex适配,封装了一个类:代码如下:
ScreenTool.h
#import#import #defineViewSafeAreaInsets(view)[ScreenToolSharedInstancegetViewSafeAreaInsets:view] #defineWindowSafeAreaInsets[ScreenToolSharedInstancegetWindowSafeAreaInsets] #defineScreen_height[[UIScreenmainScreen]bounds].size.height #defineScreen_width[[UIScreenmainScreen]bounds].size.width #defineScreenToolSharedInstance[ScreenToolsharedInstance] #defineNavAndStatusBarHeight[ScreenToolSharedInstancegetNavAndStatusBarHeight] #defineTabBarAndVirtualHomeHeight[ScreenToolSharedInstancegetTabBarAndVirtualHomeHeight] #defineStatusBarHeight[ScreenToolSharedInstancegetStatusBarHeight] #defineNavContentHeight[ScreenToolSharedInstancegetNavContentHeight] #defineTabBarContentHeight[ScreenToolSharedInstancegetTabBarContentHeight] #defineScrollViewContentInsetAdjustmentBehavior(scrollView)[ScreenToolSharedInstancesetScrollViewContentInsetAdjustmentBehavior:scrollView] typedefNS_ENUM(NSUInteger,DeviceScreenType){//设备屏幕类型 DeviceTypeIphone4Screen, DeviceTypeIphone5Screen, DeviceTypeIphone6Screen, DeviceTypeIphone6PlusScreen, DeviceTypeIphoneXScreen, DeviceTypeOtherScreen }; typedefNS_ENUM(NSUInteger,DeviceOrientationType){//屏幕方向类型 DeviceOrientationTypeHorizontalScreen, DeviceOrientationTypeVerticalScreen, DeviceOrientationTypeOther }; @interfaceScreenTool:NSObject @property(nonatomic,unsafe_unretained)BOOLisAccordingToSafeArea; -(void)setScrollViewContentInsetAdjustmentBehavior:(UIScrollView*)scrollView; +(ScreenTool*)sharedInstance; +(BOOL)isSmallScreen; -(UIEdgeInsets)getWindowSafeAreaInsets; -(UIEdgeInsets)getViewSafeAreaInsets:(UIView*)view; -(NSString*)getDevice; -(DeviceScreenType)getDeviceType; -(DeviceOrientationType)getDeviceOrientationType; -(CGFloat)getNavAndStatusBarHeight; -(CGFloat)getTabBarAndVirtualHomeHeight; -(CGFloat)getTabBarContentHeight; -(CGFloat)getNavContentHeight; -(CGFloat)getStatusBarHeight; @end ScreenTool.m [objc]viewplaincopy #import"ScreenTool.h" @implementationScreenTool -(instancetype)init{ if(self=[superinit]){ self.isAccordingToSafeArea=YES; } returnself; } +(ScreenTool*)sharedInstance{ staticdispatch_once_tpred=0; __strongstaticidscreenTool=nil; dispatch_once(&pred,^{ screenTool=[[selfalloc]init]; }); returnscreenTool; } -(void)setScrollViewContentInsetAdjustmentBehavior:(UIScrollView*)scrollView{ #if__IPHONE_OS_VERSION_MAX_ALLOWED>=110000 if(@available(iOS11.0,*)){ if([scrollViewrespondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]){ scrollView.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever; } } #endif } -(UIEdgeInsets)getWindowSafeAreaInsets{ UIEdgeInsetsi=UIEdgeInsetsZero; #if__IPHONE_OS_VERSION_MAX_ALLOWED>=110000 if(@available(iOS11.0,*)){ i=[UIApplicationsharedApplication].keyWindow.safeAreaInsets; } #endif returni; } -(UIEdgeInsets)getViewSafeAreaInsets:(UIView*)view{ UIEdgeInsetsi=UIEdgeInsetsZero; #if__IPHONE_OS_VERSION_MAX_ALLOWED>=110000 if(@available(iOS11.0,*)){ i=view.safeAreaInsets; } #endif returni; } -(NSString*)getDevice{ if((Screen_width==320&&Screen_height==480)||(Screen_height==320&&Screen_width==480)){ return@"4"; }elseif((Screen_width==320&&Screen_height==568)||(Screen_height==320&&Screen_width==568)){ return@"5"; }elseif((Screen_width==375&&Screen_height==667)||(Screen_height==375&&Screen_width==667)){ return@"6"; }elseif((Screen_width==375&&Screen_height==812)||(Screen_height==375&&Screen_width==812)){ return@"x"; }elseif((Screen_width==414&&Screen_height==736)||(Screen_height==414&&Screen_width==736)){ return@"6p"; }else{ return@""; } } -(DeviceScreenType)getDeviceType{ if((Screen_width==320&&Screen_height==480)||(Screen_height==320&&Screen_width==480)){ returnDeviceTypeIphone4Screen; }elseif((Screen_width==320&&Screen_height==568)||(Screen_height==320&&Screen_width==568)){ returnDeviceTypeIphone5Screen; }elseif((Screen_width==375&&Screen_height==667)||(Screen_height==375&&Screen_width==667)){ returnDeviceTypeIphone6Screen; }elseif((Screen_width==375&&Screen_height==812)||(Screen_height==375&&Screen_width==812)){ returnDeviceTypeIphoneXScreen; }elseif((Screen_width==414&&Screen_height==736)||(Screen_height==414&&Screen_width==736)){ returnDeviceTypeIphone6PlusScreen; }else{ returnDeviceTypeOtherScreen; } } -(DeviceOrientationType)getDeviceOrientationType{ if([[UIDevicecurrentDevice]orientation]==UIDeviceOrientationPortrait||[[UIDevicecurrentDevice]orientation]==UIDeviceOrientationPortraitUpsideDown){ returnDeviceOrientationTypeVerticalScreen; }elseif([[UIDevicecurrentDevice]orientation]==UIDeviceOrientationLandscapeLeft||[[UIDevicecurrentDevice]orientation]==UIDeviceOrientationLandscapeRight){ returnDeviceOrientationTypeHorizontalScreen; }else{ returnDeviceOrientationTypeOther; } } +(BOOL)isSmallScreen{ if(Screen_width>=375&&Screen_height>=667){ returnNO; }else{ returnYES; } } -(CGFloat)getTabBarContentHeight{ if(!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero,WindowSafeAreaInsets)){ //if([selfgetDeviceType]==DeviceTypeIphoneXScreen){ if([selfgetDeviceOrientationType]==DeviceOrientationTypeHorizontalScreen){ if(self.isAccordingToSafeArea){ return32; }else{ return49; } }else{ return49; } }else{ return49; } } -(CGFloat)getNavContentHeight{ if(!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero,WindowSafeAreaInsets)){ //} //if([selfgetDeviceType]==DeviceTypeIphoneXScreen){ if([selfgetDeviceOrientationType]==DeviceOrientationTypeHorizontalScreen){ if(self.isAccordingToSafeArea){ return32; }else{ return44; } }else{ return44; } }else{ return44; } } -(CGFloat)getStatusBarHeight{ return[[UIApplicationsharedApplication]statusBarFrame].size.height; } -(CGFloat)getNavAndStatusBarHeight{ return[selfgetNavContentHeight]+[selfgetStatusBarHeight]; } -(CGFloat)getTabBarAndVirtualHomeHeight{ return[selfgetTabBarContentHeight]+WindowSafeAreaInsets.bottom; } @end
总结
以上所述是小编给大家介绍的ios11和iphonex的相关适配问题及解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!