适配iOS11&iPhone X

作者: minjing_lin | 来源:发表于2017-09-22 18:24 被阅读316次
    适配iOS11&iPhone X

    1. shell script invocation error

    图1.png
    解决:Targets -> Bulid Phases -> Copy Pods Resources
    勾选️ Run script only when installing
    图2.png

    2.iPhoneX启动图 适配 (iPhone X 屏幕上下有黑道,没有沾满全屏)

    之前的APP在iPhoneX屏幕没填充满,上下有黑色区域,应该是你的app之前未用LaunchScreen.Storyboard作为启动页面,可以使用LaunchScreen来当做入场页面,这样APP才会自动适配为iPhoneX的大小。或者新建一个Assets中的LaunchImage来代替原来的,添加iPhoneX的尺寸图(1125*2436)。


    图3.png

    3.APPIcon

    新版本必须使用Xcode9里面的APPIcon(1024*1024),来代替 iTunes connect 里面的APPStoreIcon


    图4

    4.安全区域 适配 (Safe Area)
    状态栏(statusBar)由原来的高度20,变为了44;之前一直使用的定值64,也变成了88.

    图5.png

    解决:

    /// iPhone X
    #define  MJiPhoneX (LSCREENW == 375.f && LSCREENH == 812.f ? YES : NO)
    
    /// Status bar
    #define MJStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
    /// navigation bar
    #define MJNavBarHeight self.navigationController.navigationBar.frame.size.height
    ///  Status bar & navigation bar height
    #define MJStatusAndNavHeight (MJStatusBarHeight + MJNavBarHeight)
    
    /// Tabbar height.
    #define  MJTabbarHeight (MJiPhoneX ? (49.f+34.f) : 49.f)
    /// Tabbar safe bottom margin.
    #define  MJTabbarSafeBottomMargin (MJiPhoneX ? 34.f : 0.f)
    

    5. automaticallyAdjustsScrollViewInsets 过期问题

    由于iOS 11废弃了UIViewController的automaticallyAdjustsScrollViewInsets属性,而新增了contentInsetAdjustmentBehavior属性。
    解决:

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

    对于大量页面需要设置contentInsetAdjustmentBehavior属性,由于UIView及其子类都遵循UIAppearance协议,仅需在appdelegate 里边设置就可全局适配.

     if (@available(iOS 11.0,*)) {
            [[UIScrollView appearance]setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
        }
    

    6.UITableView中的sectionHeader或者Footer显示不正常

    图6.png

    如果tableView 是Gruop类型的话,会发现某些界面tableView的sectionHeader、sectionFooter高度与设置不符的问题。
    原因:在iOS11中如果不实现-tableView: viewForHeaderInSection:-tableView: viewForFooterInSection:,则它们都会变成默认高度,因为tableView在iOS11默认使用Self-Sizing,tableView的estimatedRowHeightestimatedSectionHeaderHeightestimatedSectionFooterHeight三个高度估算属性都由默认的0变成了UITableViewAutomaticDimension,
    解决:实现对应方法或把这三个属性设为0。如:

    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 6.0f;
    }
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        return [[UIView alloc]init];
    }
    

    快速实现:

    ios11 适配.png

    7.打印线程问题:Main Thread Checker: UI API called on a background thread:

    Main Thread Checker: UI API called on a background thread: -[UIApplication registerForRemoteNotifications]
    PID: 3083, TID: 1824050, Thread name: (none), Queue name: com.apple.usernotifications.UNUserNotificationServiceConnection.call-out, QoS: 0
    

    原因:需要在主线程执行的代码 被放在了子线程里边。检查工程中,是否在后台线程(非主线程)调用 AppKit、UIKit相关的API,比如iOS 10+ 请求通知权限时,[application registerForRemoteNotifications]在回调非主线程中执行,则Xcode 9会报上述错误。

    图7.png

    应该为:

    dispatch_async(dispatch_get_main_queue(), ^{
                [application registerForRemoteNotifications];
      });
    

    8.消除block警告
    我们定义一个不带参数的block,通常是如下的方式
    typedef void (^Block)();
    在xcode9中会提示一个警告

    This block declaration is not a prototype
    Insert ‘void'
    

    解决:
    typedef void (^Block)(void);

    9.相册权限变更
    iOS11以前:
    NSPhotoLibraryUsageDescription:访问相册和存储照片到相册(读写),会出现用户授权;
    iOS11之后:
    NSPhotoLibraryUsageDescription:无需添加。默认开启访问相册权限(读),无需用户授权;
    NSPhotoLibraryAddUsageDescription: 添加内容到相册(写),会出现用户授权;

    附:(iOS设备无线调试)
    手机连接上Xcode,打开Xcode菜单:Windows->Device and Simulators。
    找到连接上的设备,把Connect via network选项打勾。
    Xcode和设备配对完成后,在左列的设备旁边就会出现一个网络图标。

    附图.png

    断开数据线运行,发现跑不起来。尚需最后一步,在上图的设备列表中,选中手机右键,在出来的选项卡中选择一个Connect via IP Address项。选择之后输入手机的局域网ip地址。

    然后就可以愉快的玩耍了~

    相关文章

      网友评论

        本文标题:适配iOS11&iPhone X

        本文链接:https://www.haomeiwen.com/subject/uexfextx.html