iOS开发适配iOS11&iPhone X 、Xcode9遇到的坑

1.tableView向下偏移,部分UIScrollView布局出现混乱。

原因:
iOS11弃用了automaticallyAdjustsScrollViewInsets 使用了contentInsetAdjustmentBehavior代替。

解决方法如下,其中@available(iOS 11.0, *)表示在iOS11版本以上可用。

1
2
3
4
5
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}else{
self.automaticallyAdjustsScrollViewInsets = NO;
}

当然我们可以定义个宏

1
2
3
4
5
6
7
8
9
#define  KAdjustsScrollViewInsets_NO(scrollView,vc)\
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \
if (@available(iOS 11.0,*)) {\
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;\
} else {\
self.automaticallyAdjustsScrollViewInsets = NO;\
}\
_Pragma("clang diagnostic pop") \

2.TableView的区头、区尾、cell高度变大。

原因:因为TaBleViewestimatedRowHeightestimatedSectionHeaderHeightestimatedSectionFooterHeight三个高度由默认的0变成了UITableViewAutomaticDimension,导致了高度计算不对。

解决方法:

在相应界面,将三个属性都设为0

1
2
3
4
5
if (@available(iOS 11.0, *)) {
_tableView.estimatedRowHeight = 0;
_tableView.estimatedSectionHeaderHeight = 0;
_tableView.estimatedSectionFooterHeight = 0;
}

为了方便我们也可以定义宏

1
2
3
4
5
6
#define TableViewCloseTheEstimate(tableView)\
if (@available(iOS 11.0, *)) {\
tableView.estimatedRowHeight = 0;\
tableView.estimatedSectionHeaderHeight = 0;\
tableView.estimatedSectionFooterHeight = 0;\
}

当然我们如果想全局都将这三个属性设为0,也可以在AppDelegate.m 中进行全局设置。

1
2
3
4
5
if (@available(iOS 11.0, *)) {
[UITableView appearance].estimatedRowHeight = 0;
[UITableView appearance].estimatedSectionHeaderHeight = 0;
[UITableView appearance].estimatedSectionFooterHeight = 0;
}

3.部分Block警告

image

例如:

image

原因:定义未带参数的Block时,Xcode9中会报上面错误。

解决方法:

  • 参数位置为 void
  • 部分第三方也有此种警告。你不可能一个个去修改修改,所以我们需要在Build Setting ——> Other Warning Flags 中添加:-Wno-strict-prototypes,然后再CleanBuild。这种方法只是屏蔽警告……

image

4.ReactiveCocoa Unknown warning group '-Wreceiver-is-weak',ignored 警告

原因:之前的Xcode中如果消息的接收者是一个weak对象时,clang编译器会报 receiver-is-weak警告,所以在ReactiveCocoa中添加了下方的push&pop以消除警告。而在Xcode9clang已经把这个警告给移除,所以再添加下方push&pop就会有警告。

1
2
3
4
5
6
7
8
#define RACObserve(TARGET, KEYPATH) \
({ \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wreceiver-is-weak\"") \
__weak id target_ = (TARGET); \
[target_ rac_valuesForKeyPath:@keypath(TARGET, KEYPATH) observer:self]; \
_Pragma("clang diagnostic pop") \
})

解决方法:

pod 'ReactiveCocoa', '2.5'换成下方。

1
pod 'ReactiveCocoa', :git => 'https://github.com/zhao0/ReactiveCocoa.git', :tag => '2.5.2'

5. 友盟社会化组件在Xcode9上无法编译。

1
Pods/UMengSocialCOM/Umeng_SDK_Social_iOS_ARM64_5.2.1/UMSocial_Sdk_5.2.1/SocialSDKXib/UMSCommentDetailController.xib: warning: Internationalization is not available when compiling for targets before iOS 6.0

原因:之前引用的pod 'UMengSocialCOM', '~> 5.2.1',此版本友盟已不再维护,替换成UMengUShare
因项目中只使用了微信微博QQ的登录和分享,所以Podfile文件中将 pod 'UMengSocialCOM', '~> 5.2.1' 替换为👇

1
2
3
4
5
6
7
8
#友盟社会化组件
pod 'UMengUShare/Network', '~> 6.4.5'
pod 'UMengUShare/Core', '~> 6.4.5'
pod 'UMengUShare/UI', '~> 6.4.5'
pod 'UMengUShare/Plugin', '~> 6.4.5'
pod 'UMengUShare/Social/WeChat', '~> 6.4.5'
pod 'UMengUShare/Social/QQ', '~> 6.4.5'
pod 'UMengUShare/Social/Sina', '~> 6.4.5'

同时为了后期的维护,我们对所需要用到的接口进行了二次封装;

6.iPhone XTabBar下方多了一条透明区域。

原因: iPhone X 的底部是预留给系统功能的一个区域 Home Indicator, 高度34pt。如果使用的系统的TabBar,那么Home Indicator就会延展相应的barTintColor,而我们使用是自定义的。

解决方法:
我们在所以ViewContrller继承的基类BaseViewController中添加了self.edgesForExtendedLayout = UIRectEdgeNone;

1
2
3
4
5
6
7
- (instancetype) init {
self = [super init];
if (self) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
return self;
}

修改完后,布局可能有所变化。
就比如我们的一些布局为:make.top.equalTo(self.view.top).offset(kNavigationAndStatusBarHeight); 需要改为:make.top.equalTo(self.view.top)

7.iPhone X中状态栏高度变高

原因: iPhone X 全面屏,增加了刘海儿,高度从之前的20pt变成了44pt
所以我们定义宏的时候就不能直接写死。

解决方法:

1
#define kStatusBarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height)

同时补上定义的其他宏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#define kStatusBarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height)
#define kNavigationBarHeight 44.f
#define kNavigationAndStatusBarHeight (kStatusBarHeight + 44.f)
#define kTabBarHeight 49.f
#define KiPhoneXTabBarHeight (34.f + 49.f)
#define KHomeIndicatorHeight 34.f


#define kSCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define kSCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

// iPhone5 or iPhone5s
#define iPhone5_5s_SE (kSCREEN_WIDTH == 320.f && kSCREEN_HEIGHT == 568.f)

//iPhone6 or iPhone6s or iPhone7 or iPhone8
#define iPhone6_6s_7_8 (kSCREEN_WIDTH == 375.f && kSCREEN_HEIGHT == 667.f)

//iPhone6Plus or iPhone6sPlus or iPhone7Plus or iPhone8Plus
#define iPhone6Plus_6sPlus_7Plus_8Plus (kSCREEN_WIDTH == 414.f && kSCREEN_HEIGHT == 736.f)

//iPhone X
#define iPhoneX (kSCREEN_WIDTH == 375.f && kSCREEN_HEIGHT == 812.f)

参考资料: