适配iOS 11,iPhone X总结

2018-09-06  本文已影响0人  Arthur澪
-.jpg

适配iOS 11

判断iOS 11+
if (@available(iOS 11.0, *)) {

}
安全区

iOS 7 开始,在UIViewController中引入的 topLayoutGuidebottomLayoutGuide,在 iOS 11 中被废弃了。取而代之的是safeArea的概念。

UITableView

在iOS11,tableView默认使用Self-Sizing自适应尺寸(Headers, footers, and cells都默认开启了,iOS11之前为0 )。若未实现-tableView: viewForHeaderInSection:等有关高度的代理方法,系统会自动设置成默认高度UITableViewAutomaticDimension。解决:

// iOS11下不想使用Self-Sizing的话,可以通过以下方式关闭
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

PS: 在iOS8引入Self-Sizing 之后,我们可以通过实现estimatedRowHeight相关的属性来展示动态的内容,实现了estimatedRowHeight属性后,得到的初始contenSize是个估算值,是通过estimatedRowHeight x cell的个数得到的,并不是最终的contenSize,tableView不会一次性计算所有的cell的高度了,只会计算当前屏幕能够显示的cell个数再加上几个,滑动时,tableView不停地得到新的cell,更新自己的contenSize,在滑到最后的时候,会得到正确的contenSize。

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 34, 0);
self.tableView.scrollIndicatorInsets =  UIEdgeInsetsMake(0, 0, 34, 0);
搜索框

iOS 11 新增属性:navigationItem.searchController
navigationItem.hidesSearchBarWhenScrolling //滑动时是否隐藏搜索框
把你的UISearchController赋值给navigationItem,就可以实现将UISearchController集成到Navigation

页面偏移

iOS11弃用了automaticallyAdjustsScrollViewInsets属性,automaticallyAdjustsScrollViewInsets = NO就等于没有设置(默认是YES)于是页面顶部就多了一定的contentInset;取而代之的是UIScrollView新增了contentInsetAdjustmentBehavior属性

因此,所有继承自UIScrollView及其子类的,都需要设置 contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever
可以进行全局配置:

if (@available(iOS 11.0, *)) {
    [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];     
} else {
    [UIScrollView appearance].contentInsetAdjustmentBehavior = NO;
}
pickerControllder.navigationBar.translucent=NO;
位置权限
    <key>NSLocationUsageDescription</key>
    <string>获取地理位置,精准推送服务</string>
    <!-- 在使用期间访问位置 -->
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>获取地理位置,精准推送服务</string>
    <!-- 始终访问位置 -->
    <key>NSLocationAlwaysUsageDescription</key>
    <string>App需要您的同意,才能始终访问位置</string>
    <!-- iOS 11访问位置 -->
    <key>NSLocationAlwaysAndWhenInUseUsageDeion</key>
    <string>App需要您的同意,才能始终访问位置</string>
相册访问权限
Push过程中TabBar位置上移问题

解决:在UINavigationController的基类重写代理方法pushViewController,在Push时重置TabBarframe

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
    // 修改
    CGRect frame = self.tabBarController.tabBar.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
    self.tabBarController.tabBar.frame = frame;
}

适配iPhone X

屏幕尺寸对比

-.jpg

状态栏高度,底部安全距离

--.jpg

横屏(Landscape)

-.jpg
-.jpg
定义常用宏
// 是否iPhoneX
#define kScreenIphoneX [UIScreen mainScreen].bounds.size.height>=812
// 底部安全距离
#define kBottomSafeMargin (kScreenIphoneX ? 34.f : 0.f)
// 状态栏高度
#define kStatusBarHeight  (kScreenIphoneX ? 44.f : 20.f)
// 导航栏的最大y值
#define kNavigaBarHeight  (kScreenIphoneX ? 88.f : 64.f)
NavBar新特性

在iOS 11中,导航栏新加入了largeTitlessearchController两个新特性。

--.jpg
self.navigationController?.navigationBar.prefersLargeTitles = YES

navigationItem.largeTitleDisplayMode //设置显示时机

旧工程适配

只需要在LaunchImage中添加一个尺寸为1125 × 2436的启动图,并且工程使用LaunchImage加载启动图的,而不是使用 LaunchImage.storeBorad

iOS12、 iPhone XS Max 、XR适配,Xcode10配置

苹果新品发布会完了,准备填坑

iPhone XSX的屏幕尺寸一样,因此只需视频XRXS Max
如果只设置iPhone X的启动图,iPhone XR/XS Max会默认使用iPhoneX的启动图。

31538209431_.pic.jpg

1、宏

#define isIPhoneXSeries     ([UIScreen instancesRespondToSelector:@selector(currentMode)] ?\
(\
CGSizeEqualToSize(CGSizeMake(375, 812),[UIScreen mainScreen].bounds.size)\
 ||\
CGSizeEqualToSize(CGSizeMake(414, 896),[UIScreen mainScreen].bounds.size)\
)\
:\
NO)

2、内联函数

static inline BOOL isIPhoneXSeries() {
    BOOL iPhoneXSeries = NO;
    if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
        return iPhoneXSeries;
    }

    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            iPhoneXSeries = YES;
        }
    }

    return iPhoneXSeries;
}

如果你的工程中如果依赖libstdc++,无论是你本身的功能用 C++ 跨平台编写,还是你引入了某个SDK其内部依赖这个libstdc++,都会导致整个工程编译不通过,报出Undefined symbols,C++ 的 List 找不到了。

原因是苹果在XCode10和iOS12中移除了libstdc++这个库,由libc++这个库取而代之,苹果的解释是libstdc++已经标记为废弃有5年了,建议大家使用经过了llvm优化过并且全面支持C++11的libc++库。

解决:拷贝缺失的libstdc++

/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/lib/

/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/lib/


要记得拷贝2套,一套是模拟器的,一套是设备的。

上一篇 下一篇

猜你喜欢

热点阅读