欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios 11和iphone x的相关适配问题及解决方法

程序员文章站 2023-12-17 20:59:22
有关ios11 ,最大的变化就是增加了一个安全区域(safearea)的概念,ios11 适配的大部分问题都是由于它引起的。 在ios 11中,tableview会莫名偏...

有关ios11 ,最大的变化就是增加了一个安全区域(safearea)的概念,ios11 适配的大部分问题都是由于它引起的。

在ios 11中,tableview会莫名偏移,解决办法:

//解决ios11 tableview会出现漂移,预估高度都设为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(ios 11.0, *)) { 
  if ([scrollview respondstoselector:@selector(setcontentinsetadjustmentbehavior:)]) { 
   scrollview.contentinsetadjustmentbehavior = uiscrollviewcontentinsetadjustmentnever; 
  } 
 } 
#endif 
} 

关于iphone x适配,封装了一个类:代码如下:

screentool.h

#import <foundation/foundation.h> 
#import <uikit/uikit.h> 
#define viewsafeareainsets(view) [screentoolsharedinstance getviewsafeareainsets:view] 
#define windowsafeareainsets [screentoolsharedinstance getwindowsafeareainsets] 
#define screen_height [[uiscreen mainscreen] bounds].size.height 
#define screen_width [[uiscreen mainscreen] bounds].size.width 
#define screentoolsharedinstance [screentool sharedinstance] 
#define navandstatusbarheight [screentoolsharedinstance getnavandstatusbarheight] 
#define tabbarandvirtualhomeheight [screentoolsharedinstance gettabbarandvirtualhomeheight] 
#define statusbarheight [screentoolsharedinstance getstatusbarheight] 
#define navcontentheight [screentoolsharedinstance getnavcontentheight] 
#define tabbarcontentheight [screentoolsharedinstance gettabbarcontentheight] 
#define scrollviewcontentinsetadjustmentbehavior(scrollview) [screentoolsharedinstance setscrollviewcontentinsetadjustmentbehavior:scrollview] 
typedef ns_enum(nsuinteger, devicescreentype) {//设备屏幕类型 
 devicetypeiphone4screen, 
 devicetypeiphone5screen, 
 devicetypeiphone6screen, 
 devicetypeiphone6plusscreen, 
 devicetypeiphonexscreen, 
 devicetypeotherscreen 
}; 
typedef ns_enum(nsuinteger, deviceorientationtype) {//屏幕方向类型 
 deviceorientationtypehorizontalscreen, 
 deviceorientationtypeverticalscreen, 
 deviceorientationtypeother 
}; 
@interface screentool : nsobject 
@property(nonatomic,unsafe_unretained)bool isaccordingtosafearea; 
-(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] view plain copy
#import "screentool.h" 
@implementation screentool 
-(instancetype)init{ 
 if (self = [super init]) { 
  self.isaccordingtosafearea = yes; 
 } 
 return self; 
} 
+(screentool *)sharedinstance { 
 static dispatch_once_t pred = 0; 
 __strong static id screentool = nil; 
 dispatch_once(&pred, ^{ 
  screentool = [[self alloc] init]; 
 }); 
 return screentool; 
} 
-(void)setscrollviewcontentinsetadjustmentbehavior:(uiscrollview *)scrollview { 
#if __iphone_os_version_max_allowed >= 110000 
 if(@available(ios 11.0, *)) { 
  if ([scrollview respondstoselector:@selector(setcontentinsetadjustmentbehavior:)]) { 
   scrollview.contentinsetadjustmentbehavior = uiscrollviewcontentinsetadjustmentnever; 
  } 
 } 
#endif 
} 
-(uiedgeinsets)getwindowsafeareainsets { 
 uiedgeinsets i = uiedgeinsetszero; 
#if __iphone_os_version_max_allowed >= 110000 
 if(@available(ios 11.0, *)) { 
  i = [uiapplication sharedapplication].keywindow.safeareainsets; 
 } 
#endif 
 return i; 
} 
-(uiedgeinsets)getviewsafeareainsets:(uiview *)view { 
 uiedgeinsets i = uiedgeinsetszero; 
#if __iphone_os_version_max_allowed >= 110000 
 if(@available(ios 11.0, *)) { 
  i = view.safeareainsets; 
 } 
#endif 
 return i; 
} 
-(nsstring *)getdevice { 
 if ((screen_width == 320 && screen_height == 480) || (screen_height == 320 && screen_width == 480)) { 
  return @"4"; 
 }else if ((screen_width == 320 && screen_height == 568) || (screen_height == 320 && screen_width == 568)) { 
  return @"5"; 
 }else if ((screen_width == 375 && screen_height == 667) || (screen_height == 375 && screen_width == 667)) { 
  return @"6"; 
 }else if ((screen_width == 375 && screen_height == 812) || (screen_height == 375 && screen_width == 812)) { 
  return @"x"; 
 }else if ((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)) { 
  return devicetypeiphone4screen; 
 }else if ((screen_width == 320 && screen_height == 568) || (screen_height == 320 && screen_width == 568)) { 
  return devicetypeiphone5screen; 
 }else if ((screen_width == 375 && screen_height == 667) || (screen_height == 375 && screen_width == 667)) { 
  return devicetypeiphone6screen; 
 }else if ((screen_width == 375 && screen_height == 812) || (screen_height == 375 && screen_width == 812)) { 
  return devicetypeiphonexscreen; 
 }else if ((screen_width == 414 && screen_height == 736) || (screen_height == 414 && screen_width == 736)) { 
  return devicetypeiphone6plusscreen; 
 }else { 
  return devicetypeotherscreen; 
 } 
} 
-(deviceorientationtype)getdeviceorientationtype { 
 if ([[uidevice currentdevice] orientation] == uideviceorientationportrait || [[uidevice currentdevice] orientation] == uideviceorientationportraitupsidedown) { 
  return deviceorientationtypeverticalscreen; 
 } else if ([[uidevice currentdevice] orientation] == uideviceorientationlandscapeleft || [[uidevice currentdevice] orientation] == uideviceorientationlandscaperight) { 
  return deviceorientationtypehorizontalscreen; 
 }else { 
  return deviceorientationtypeother; 
 } 
} 
+(bool)issmallscreen{ 
 if (screen_width >=375 && screen_height >= 667) { 
  return no; 
 }else { 
  return yes; 
 } 
} 
-(cgfloat)gettabbarcontentheight { 
 if (!uiedgeinsetsequaltoedgeinsets(uiedgeinsetszero, windowsafeareainsets)) { 
  //if ([self getdevicetype] == devicetypeiphonexscreen) { 
  if ([self getdeviceorientationtype] == deviceorientationtypehorizontalscreen) { 
   if (self.isaccordingtosafearea) { 
    return 32; 
   }else { 
    return 49; 
   } 
  }else { 
   return 49; 
  } 
 }else { 
  return 49; 
 } 
} 
-(cgfloat)getnavcontentheight { 
 if (!uiedgeinsetsequaltoedgeinsets(uiedgeinsetszero, windowsafeareainsets)) { 
  // } 
  // if ([self getdevicetype] == devicetypeiphonexscreen) { 
  if ([self getdeviceorientationtype] == deviceorientationtypehorizontalscreen) { 
   if (self.isaccordingtosafearea) { 
    return 32; 
   }else { 
    return 44; 
   } 
  }else { 
   return 44; 
  } 
 }else { 
  return 44; 
 } 
} 
-(cgfloat)getstatusbarheight { 
 return [[uiapplication sharedapplication] statusbarframe].size.height; 
} 
-(cgfloat)getnavandstatusbarheight { 
 return [self getnavcontentheight]+[self getstatusbarheight]; 
} 
-(cgfloat)gettabbarandvirtualhomeheight { 
 return [self gettabbarcontentheight]+windowsafeareainsets.bottom; 
} 
@end 

总结

以上所述是小编给大家介绍的ios 11和iphone x的相关适配问题及解决方法,希望对大家有所帮助

上一篇:

下一篇: