iOSiOS新手学习iOS-Developer-OC

iOS-iOS 11与iPhoneX适配小结

2018-01-13  本文已影响194人  Simple_Code

一张可以让你了解iPhoneX适配的图

025edd59b9e831a801212fb71be443.png
iOS11和xcode9已经发布一段时间了,最近对项目进行的iOS 11和iPhoneX的适配,也遇到一些问题,下面列举出来、希望能够帮助正在爬坑和即将爬坑的你
一:iOS 11相关
1.iOS 11上面废除了automaticallyAdjustsScrollViewInsets,以前的代码有的会下滑64

方法1. 如果你想全局配置、只需在APP初始化的时候添加一下代码

#define IOS11   @available(iOS 11.0, *)
if (IOS11) {
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    }else {
    self.automaticallyAdjustsScrollViewInsets = NO;
}

1.2如果你想在某个controller做修改、需要做以下处理

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

2.tableView的头部试图和尾部试图

在iOS11里面有时候在tableView的头部和尾部留白,因为苹果给滚动试图加进去了self-sizeing,开始计算逐步计算contentSize,默认如果不去实现viewForHeaderInSection就不会调用heightForHeaderInSection,尾部试图一样。

如果你不想实现viewForHeaderInSection也不想留白,那么只需要你把self-sizeing自动估高关闭即可

自动关闭估算高度,不想估算那个,就设置那个即可
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

补充tableView相关 -(以后再也不用写 return 0.001了,使用系统的宏更方便、大气)

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
二:适配iPhoneX相关
1.使用纯代码动态获取Navigationbar以上的高度

如果希望从navigationbar以下布局可以使用以下代码设置控件的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.使用纯代码动态获取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.根据屏幕宽度比、适配不同机型
#define PCBScreenWidthRatio      (PCBScreen_Width / 375.0)
#define PCBAdapted_Width(x)      (ceilf((x) * PCBScreenWidthRatio))
4.如果使用了Masonry 进行布局,就要适配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);
        }
    }];

以上是我在适配过程中遇到的一些问题、后续会跟进、希望帮到你们……

上一篇下一篇

猜你喜欢

热点阅读