查看: 445|回复: 0

[IOS开发教程] ios 11和iphone x的相关适配问题及解决方法

发表于 2017-11-29 08:00:04

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

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

  1. //解决iOS11 tableview会出现漂移,预估高度都设为0
  2. self.tableView.estimatedRowHeight = 0;
  3. self.tableView.estimatedSectionHeaderHeight = 0;
  4. self.tableView.estimatedSectionFooterHeight = 0;
复制代码

解决scrollView,默认位移了,解决办法:

  1. -(void)setScrollViewContentInsetAdjustmentBehavior:(UIScrollView *)scrollView {
  2. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  3. if(@available(iOS 11.0, *)) {
  4. if ([scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
  5. scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  6. }
  7. }
  8. #endif
  9. }
复制代码

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

ScreenTool.h

  1. #import <Foundation/Foundation.h>
  2. #import <UIKit/UIKit.h>
  3. #define ViewSafeAreaInsets(view) [ScreenToolSharedInstance getViewSafeAreaInsets:view]
  4. #define WindowSafeAreaInsets [ScreenToolSharedInstance getWindowSafeAreaInsets]
  5. #define Screen_height [[UIScreen mainScreen] bounds].size.height
  6. #define Screen_width [[UIScreen mainScreen] bounds].size.width
  7. #define ScreenToolSharedInstance [ScreenTool sharedInstance]
  8. #define NavAndStatusBarHeight [ScreenToolSharedInstance getNavAndStatusBarHeight]
  9. #define TabBarAndVirtualHomeHeight [ScreenToolSharedInstance getTabBarAndVirtualHomeHeight]
  10. #define StatusBarHeight [ScreenToolSharedInstance getStatusBarHeight]
  11. #define NavContentHeight [ScreenToolSharedInstance getNavContentHeight]
  12. #define TabBarContentHeight [ScreenToolSharedInstance getTabBarContentHeight]
  13. #define ScrollViewContentInsetAdjustmentBehavior(scrollView) [ScreenToolSharedInstance setScrollViewContentInsetAdjustmentBehavior:scrollView]
  14. typedef NS_ENUM(NSUInteger, DeviceScreenType) {//设备屏幕类型
  15. DeviceTypeIphone4Screen,
  16. DeviceTypeIphone5Screen,
  17. DeviceTypeIphone6Screen,
  18. DeviceTypeIphone6PlusScreen,
  19. DeviceTypeIphoneXScreen,
  20. DeviceTypeOtherScreen
  21. };
  22. typedef NS_ENUM(NSUInteger, DeviceOrientationType) {//屏幕方向类型
  23. DeviceOrientationTypeHorizontalScreen,
  24. DeviceOrientationTypeVerticalScreen,
  25. DeviceOrientationTypeOther
  26. };
  27. @interface ScreenTool : NSObject
  28. @property(nonatomic,unsafe_unretained)BOOL isAccordingToSafeArea;
  29. -(void)setScrollViewContentInsetAdjustmentBehavior:(UIScrollView *)scrollView;
  30. +(ScreenTool *)sharedInstance;
  31. +(BOOL)isSmallScreen;
  32. -(UIEdgeInsets)getWindowSafeAreaInsets;
  33. -(UIEdgeInsets)getViewSafeAreaInsets:(UIView *)view;
  34. -(NSString *)getDevice;
  35. -(DeviceScreenType)getDeviceType;
  36. -(DeviceOrientationType)getDeviceOrientationType;
  37. -(CGFloat)getNavAndStatusBarHeight;
  38. -(CGFloat)getTabBarAndVirtualHomeHeight;
  39. -(CGFloat)getTabBarContentHeight;
  40. -(CGFloat)getNavContentHeight;
  41. -(CGFloat)getStatusBarHeight;
  42. @end
  43. ScreenTool.m
  44. [objc] view plain copy
  45. #import "ScreenTool.h"
  46. @implementation ScreenTool
  47. -(instancetype)init{
  48. if (self = [super init]) {
  49. self.isAccordingToSafeArea = YES;
  50. }
  51. return self;
  52. }
  53. +(ScreenTool *)sharedInstance {
  54. static dispatch_once_t pred = 0;
  55. __strong static id screenTool = nil;
  56. dispatch_once(&pred, ^{
  57. screenTool = [[self alloc] init];
  58. });
  59. return screenTool;
  60. }
  61. -(void)setScrollViewContentInsetAdjustmentBehavior:(UIScrollView *)scrollView {
  62. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  63. if(@available(iOS 11.0, *)) {
  64. if ([scrollView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
  65. scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  66. }
  67. }
  68. #endif
  69. }
  70. -(UIEdgeInsets)getWindowSafeAreaInsets {
  71. UIEdgeInsets i = UIEdgeInsetsZero;
  72. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  73. if(@available(iOS 11.0, *)) {
  74. i = [UIApplication sharedApplication].keyWindow.safeAreaInsets;
  75. }
  76. #endif
  77. return i;
  78. }
  79. -(UIEdgeInsets)getViewSafeAreaInsets:(UIView *)view {
  80. UIEdgeInsets i = UIEdgeInsetsZero;
  81. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  82. if(@available(iOS 11.0, *)) {
  83. i = view.safeAreaInsets;
  84. }
  85. #endif
  86. return i;
  87. }
  88. -(NSString *)getDevice {
  89. if ((Screen_width == 320 && Screen_height == 480) || (Screen_height == 320 && Screen_width == 480)) {
  90. return @"4";
  91. }else if ((Screen_width == 320 && Screen_height == 568) || (Screen_height == 320 && Screen_width == 568)) {
  92. return @"5";
  93. }else if ((Screen_width == 375 && Screen_height == 667) || (Screen_height == 375 && Screen_width == 667)) {
  94. return @"6";
  95. }else if ((Screen_width == 375 && Screen_height == 812) || (Screen_height == 375 && Screen_width == 812)) {
  96. return @"x";
  97. }else if ((Screen_width == 414 && Screen_height == 736) || (Screen_height == 414 && Screen_width == 736)) {
  98. return @"6p";
  99. }else {
  100. return @"";
  101. }
  102. }
  103. -(DeviceScreenType)getDeviceType {
  104. if ((Screen_width == 320 && Screen_height == 480) || (Screen_height == 320 && Screen_width == 480)) {
  105. return DeviceTypeIphone4Screen;
  106. }else if ((Screen_width == 320 && Screen_height == 568) || (Screen_height == 320 && Screen_width == 568)) {
  107. return DeviceTypeIphone5Screen;
  108. }else if ((Screen_width == 375 && Screen_height == 667) || (Screen_height == 375 && Screen_width == 667)) {
  109. return DeviceTypeIphone6Screen;
  110. }else if ((Screen_width == 375 && Screen_height == 812) || (Screen_height == 375 && Screen_width == 812)) {
  111. return DeviceTypeIphoneXScreen;
  112. }else if ((Screen_width == 414 && Screen_height == 736) || (Screen_height == 414 && Screen_width == 736)) {
  113. return DeviceTypeIphone6PlusScreen;
  114. }else {
  115. return DeviceTypeOtherScreen;
  116. }
  117. }
  118. -(DeviceOrientationType)getDeviceOrientationType {
  119. if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {
  120. return DeviceOrientationTypeVerticalScreen;
  121. } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) {
  122. return DeviceOrientationTypeHorizontalScreen;
  123. }else {
  124. return DeviceOrientationTypeOther;
  125. }
  126. }
  127. +(BOOL)isSmallScreen{
  128. if (Screen_width >=375 && Screen_height >= 667) {
  129. return NO;
  130. }else {
  131. return YES;
  132. }
  133. }
  134. -(CGFloat)getTabBarContentHeight {
  135. if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, WindowSafeAreaInsets)) {
  136. //if ([self getDeviceType] == DeviceTypeIphoneXScreen) {
  137. if ([self getDeviceOrientationType] == DeviceOrientationTypeHorizontalScreen) {
  138. if (self.isAccordingToSafeArea) {
  139. return 32;
  140. }else {
  141. return 49;
  142. }
  143. }else {
  144. return 49;
  145. }
  146. }else {
  147. return 49;
  148. }
  149. }
  150. -(CGFloat)getNavContentHeight {
  151. if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, WindowSafeAreaInsets)) {
  152. // }
  153. // if ([self getDeviceType] == DeviceTypeIphoneXScreen) {
  154. if ([self getDeviceOrientationType] == DeviceOrientationTypeHorizontalScreen) {
  155. if (self.isAccordingToSafeArea) {
  156. return 32;
  157. }else {
  158. return 44;
  159. }
  160. }else {
  161. return 44;
  162. }
  163. }else {
  164. return 44;
  165. }
  166. }
  167. -(CGFloat)getStatusBarHeight {
  168. return [[UIApplication sharedApplication] statusBarFrame].size.height;
  169. }
  170. -(CGFloat)getNavAndStatusBarHeight {
  171. return [self getNavContentHeight]+[self getStatusBarHeight];
  172. }
  173. -(CGFloat)getTabBarAndVirtualHomeHeight {
  174. return [self getTabBarContentHeight]+WindowSafeAreaInsets.bottom;
  175. }
  176. @end
复制代码

总结

以上所述是小编给大家介绍的ios 11和iphone x的相关适配问题及解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对程序员之家网站的支持!



回复

使用道具 举报

关闭

站长推荐上一条 /1 下一条