美文网首页
适配 iOS 11 & iPhone X

适配 iOS 11 & iPhone X

作者: 逆光少年 | 来源:发表于2017-09-25 14:34 被阅读347次

    ios11适配中的问题及解决办法


    1. 滚动条高度跳动、上下拉刷新问题:
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    
    2. 列表/页面偏移

    本来是这样的

    if (@available(iOS 11.0, *)){
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    

    目前发现所有的Scrollview 及其子类都需要设置 contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever ,工程中大量使用列表的同学不要慌,不要忙,因为UIView及其子类都遵循UIAppearance协议,我们可以进行全局配置:

    // AppDelegate 进行全局设置
        if (@available(iOS 11.0, *)){
            [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
        }
    

    这样一来使用UITableview 、UICollectionView、UIScrollview的时候就不需要再单独设置该属性了。

    3. 导航栏按钮位置问题

    之前这样写控制按钮的边距

        //调整按钮边距
    //    UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //    //将宽度设为负值
    //    spaceItem.width= -5;
    //    [items addObject:spaceItem];
    

    今日不同往日,此方法无效了。

    我试着使用了下面的方法

    #pragma mark ————— 导航栏 添加文字按钮 —————
    - (NSMutableArray *)addNavigationItemWithTitles:(NSArray *)titles isLeft:(BOOL)isLeft target:(id)target action:(SEL)action tags:(NSArray *)tags
    {
         
        NSMutableArray * items = [[NSMutableArray alloc] init];
         
        //调整按钮位置
    //    UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //    //将宽度设为负值
    //    spaceItem.width= -5;
    //    [items addObject:spaceItem];
         
        NSMutableArray * buttonArray = [NSMutableArray array];
        NSInteger i = 0;
        for (NSString * title in titles) {
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(0, 0, 30, 30);
            [btn setTitle:title forState:UIControlStateNormal];
            [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
            btn.titleLabel.font = SYSTEMFONT(16);
            [btn setTitleColor:KWhiteColor forState:UIControlStateNormal];
            btn.tag = [tags[i++] integerValue];
            [btn sizeToFit];
             
            //设置偏移
            if (isLeft) {
                [btn setContentEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
            }else{
                [btn setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, -10)];
            }
             
            UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];
            [items addObject:item];
            [buttonArray addObject:btn];
        }
        if (isLeft) {
            self.navigationItem.leftBarButtonItems = items;
        } else {
            self.navigationItem.rightBarButtonItems = items;
        }
        return buttonArray;
    }
    

    图层调试发现此法其实属障眼法,并不完美,设置内容偏移,其实际位置并没有发生变化,这可能导致按钮部分区域无法点击,目前偏移10像素问题不大,其他请自行测试,若有更完美的办法请联系我更新。

    4. 位置权限

    在IOS11,原有的NSLocationAlwaysUsageDeion被降级为NSLocationWhenInUseUsageDeion。因此,在原来项目中使用requestAlwaysAuthorization获取定位权限,而未在plist文件中配置NSLocationAlwaysAndWhenInUseUsageDeion,系统框不会弹出。建议新旧key值都在plist里配置,反正我试下来是没有问题,唯一的区别是使用requestAlwaysAuthorization获取权限 IOS11系统弹框会把几种权限级别全部列出,供用户选择,显然更人性化了。

    快去更新你的info.plist

       NSLocationUsageDescription
       获取地理位置,精准推送服务
       NSLocationWhenInUseUsageDescription
       获取地理位置,精准推送服务
       NSLocationAlwaysUsageDescription
       App需要您的同意,才能始终访问位置
       NSLocationAlwaysAndWhenInUseUsageDeion
       App需要您的同意,才能始终访问位置
    

    iPhone X 适配

    1.动态计算statusbar + navigationBar高度

    送你几个宏,来日好好撸,莫偷懒

    #define kStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
    #define kNavBarHeight 44.0
    #define kTabBarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?83:49)
    #define kTopHeight (kStatusBarHeight + kNavBarHeight)
    

    这样可以解决大部分因位置导致的问题

    2.UISearchbar高度在ios11上由原来的44变成56
    3.iPhoneX启动图 适配 (iPhone X 屏幕上下有黑道,没有沾满全屏)

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

    1518951-b00d1f10948e2dfd.png
    4.MJRefresh的footer出现在Home Indicator位置上
    1706847-895281fe29e07626.png

    这是应为iOS11弃用了automaticallyAdjustsScrollViewInsets属性,新增contentInsetAdjustmentBehavior来替代它。就是iOS11会自动限制CollectionView的底部不会超出安全区范围,不想系统帮我们自动设置边距则要在iOS11中加上下面这句

    if (@available(iOS 11.0, *)) {
            self.myCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
    

    但是这时候就会出现我们的CollectionView内容会超出安全区和Home Indicator重叠,这样最底下的cell两边就会被屏幕圆角切掉一部分,影响用户体验。我的解决办法是给collectionView加上一个34pt高的footer

    //iPhoneX是判断机型的宏:#define iPhoneX ([UIScreen mainScreen].bounds.size.height == 812.0)
    //SCREEN_WIDTH是屏幕宽宏:#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
    if (iPhoneX) {
        flowLayout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 34);
      }
    
    5.safeArea安全区设置无效

    这是个很奇怪的坑,我的安全区设置无效是因为项目里使用了表情云的SDK,移除该SDK或更新至适配ios11的SDK即可。

    参考:http://www.cocoachina.com/ios/20170925/20642.html
    http://www.jianshu.com/p/fd240dad1c2c

    相关文章

      网友评论

          本文标题:适配 iOS 11 & iPhone X

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