一張可以讓你了解iPhoneX適配的圖

025edd59b9e831a801212fb71be443.png
iOS11和xcode9已經(jīng)發(fā)布一段時(shí)間了,最近對(duì)項(xiàng)目進(jìn)行的iOS 11和iPhoneX的適配,也遇到一些問(wèn)題,下面列舉出來(lái)、希望能夠幫助正在爬坑和即將爬坑的你
一:iOS 11相關(guān)
1.iOS 11上面廢除了automaticallyAdjustsScrollViewInsets,以前的代碼有的會(huì)下滑64
方法1. 如果你想全局配置、只需在APP初始化的時(shí)候添加一下代碼
#define IOS11 @available(iOS 11.0, *)
if (IOS11) {
[[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
1.2如果你想在某個(gè)controller做修改、需要做以下處理
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
2.tableView的頭部試圖和尾部試圖
在iOS11里面有時(shí)候在tableView的頭部和尾部留白,因?yàn)樘O果給滾動(dòng)試圖加進(jìn)去了self-sizeing,開始計(jì)算逐步計(jì)算contentSize,默認(rèn)如果不去實(shí)現(xiàn)viewForHeaderInSection就不會(huì)調(diào)用heightForHeaderInSection,尾部試圖一樣。
如果你不想實(shí)現(xiàn)viewForHeaderInSection也不想留白,那么只需要你把self-sizeing自動(dòng)估高關(guān)閉即可
自動(dòng)關(guān)閉估算高度,不想估算那個(gè),就設(shè)置那個(gè)即可
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;
補(bǔ)充tableView相關(guān) -(以后再也不用寫 return 0.001了,使用系統(tǒng)的宏更方便、大氣)
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return CGFLOAT_MIN;
}
二:適配iPhoneX相關(guān)
1.使用純代碼動(dòng)態(tài)獲取Navigationbar以上的高度
如果希望從navigationbar以下布局可以使用以下代碼設(shè)置控件的Top
#define PCBStatusBar_Height [[UIApplication sharedApplication] statusBarFrame].size.height
#define PCBNavigationBar_Height self.navigationController.navigationBar.frame.size.height
#define PCBHeight_64 (PCBStatusBar_Height + PCBNavigationBar_Height)
2.使用純代碼動(dòng)態(tài)獲取tabbar以一下的高度
#define IPhoneX ([UIScreen mainScreen].bounds.size.width == 375.0f && [UIScreen mainScreen].bounds.size.height == 812.0f)
#define PCBTabBar_Height (IPhoneX ? 83.f : 49.f)
3.根據(jù)屏幕寬度比、適配不同機(jī)型
#define PCBScreenWidthRatio (PCBScreen_Width / 375.0)
#define PCBAdapted_Width(x) (ceilf((x) * PCBScreenWidthRatio))
4.如果使用了Masonry 進(jìn)行布局,就要適配safeArea
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
if (@available(iOS 11.0, *)) {
make.edges.mas_equalTo(self.view.safeAreaInsets);
}else{
make.edges.equalTo(self.view);
}
}];