公司项目最近要适配iPhoneX,于是搞起
1、适配启动图(Launch Image)
Launch Image作为开启app的第一个界面,决定了是否支持iPhoneX的适配,如果不对这个界面做适配就会在屏幕上下留下一段黑色,网上有人说需要用xib或者storyboad来处理,但是改为storyboard后发现横屏启动的时候很不友好。
于是继续使用Assets
![](https://img.haomeiwen.com/i3643818/bea5d88eb3c4d6e6.png)
Assets.xcassets中添加New iOS launch image
![](https://img.haomeiwen.com/i3643818/a0bbd52a58a1d17c.png)
2、状态栏(status bar)
状态栏高度不再是不变的20,需要使用
#define kStatusBarHeight CGRectGetHeight[[UIApplication sharedApplication] statusBarFrame])
3、tabbar
包含 Home Indicator,tabbar的高度不再是固定的49高度,实现如下
#define kTabbarHeight kStatusBarHeight > 20 ? 83 : 49
4、autolayout(使用Masonry)
由于多出来个齐刘海,对齐的时候再也不能直接设置offset为0了,改为
UIView *subView = [UIView new];
subView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:subView];
if (@available(iOS 11.0, *)) {
[subView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft); //应对横屏
make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);//应对横屏
make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
}];
}
else {
[subView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.offset(0);
}];
}
5、UITableView适配
在iOS11中UITableView的sectionHeader和sectoinFooter的高度突然变得不对了,并且下面几个方法不会被调用
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
这是因为在iOS11中UITableView启用了self-size,需要做如下设置
tableView.estimatedRowHeight = 0;
tableView.estimatedSectionHeaderHeight = 0;
tableView.estimatedSectionFooterHeight = 0;
6、导航条 navigation bar
1、导航栏新增了一种大标题样式,默认设置是不开启,所以不需要修改。
2、titleView支持autolayout,这要求titleView必须是能够自撑开的或实现了- intrinsicContentSize
7、ScrollView
iOS 11中ViewController的automaticallyAdjustsScrollViewInsets属性被废弃了,顶部就多了一定的inset,关于安全区域适配,简书上的这篇文章iOS 11 安全区域适配总结介绍得非常详细,请参考这篇文章。
参考文章:
适配iOS11 - UITableview UICollectionView MJRefresh下拉刷新错乱
如何适配iOS11和iPhoneX
简书App适配iOS 11
iOS 11 安全区域适配总结
iPhone X 适配
适配iOS 11与iPhoneX
网友评论