美文网首页
iOS11以及iPhone X适配

iOS11以及iPhone X适配

作者: Frankxp | 来源:发表于2017-11-25 16:09 被阅读21次

    简介

    iOS11刚出beta的时候,由于项目周期比较紧张(iOS笔者一人,摊手),所以紧急的修复了下体验严重问题的地方。
    最近一段时间终于有些时间可以认真的适配一番iOS11\iPhone X,由于项目比较庞大,而且项目中是OC和Swift混合开发,还是花掉两天时间,顺便解决了一些业务bug,顺便适配Swift4.0/(ㄒoㄒ)/~~,这里就不总结Swift4.0的适配了。另外项目使用到ARKit场景,而ARKit只能支持iOS11以上,所以同时在这方面的做了下版本兼容
    笔者项目由于iOS11影响较大的是导航栏、automaticallyAdjustsScrollViewInsets弃用、以及新的safeArea

    automaticallyAdjustsScrollViewInsets

    image.png

    对于这种隐藏导航栏,很简单约束加上safeArea加上即可

    image.png

    由于automaticallyAdjustsScrollViewInsets被废弃iOS11后改由UIScrollView 的setContentInsetAdjustmentBehavior来控制所以维持以前的行为不变这里可以
    加上一个宏

    • OC
    #define  adjustsScrollViewInsets(scrollView,vc)\
        do {\
        _Pragma("clang diagnostic push")\
        _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"")\
        if ([scrollView respondsToSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:")]) {\
            NSMethodSignature *signature = [UIScrollView instanceMethodSignatureForSelector:@selector(setContentInsetAdjustmentBehavior:)];\
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];\
            NSInteger argument = 2;\
            invocation.target = scrollView;\
            invocation.selector = @selector(setContentInsetAdjustmentBehavior:);\
            [invocation setArgument:&argument atIndex:2];\
            [invocation retainArguments];\
            [invocation invoke];\
        } else {\
            vc.automaticallyAdjustsScrollViewInsets = NO;\
        }\
        _Pragma("clang diagnostic pop")\
        } while (0)
    
    • Swift
    func adjustsScrollViewInsets_false(_ scrollView: UIScrollView, vc: UIViewController) {
            if #available(iOS 11.0, *) {
                scrollView.contentInsetAdjustmentBehavior = .never
            } else {
                vc.automaticallyAdjustsScrollViewInsets = false
            }
        }
    

    在原有self.automaticallyAdjustsScrollViewInsets = NO代码处进行替换

    关于tableview的问题

    iOS 11中如果不实现
    -tableView: viewForFooterInSection:
    -tableView: viewForHeaderInSection:,
    那么
    -tableView: heightForHeaderInSection:
    -tableView: heightForFooterInSection:
    不会被调用。
    因为iOS11中estimatedRowHeight 、estimatedSectionHeaderHeight estimatedSectionFooterHeight三个高度估算属性由默认的0变成了UITableViewAutomaticDimension,导致高度计算不对,contentSize出错,解决方法是实现对应方法或关闭UITableView的Self-Sizing。

    关闭当前_tableView
    _tableView.estimatedRowHeight = 0.;
    _tableView.estimatedSectionFooterHeight = 0.;
    _tableView.estimatedSectionHeaderHeight = 0.;
    
    关闭所有的UIScroolView、UITableView和UICollectionView的Self-Sizing
    UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    UITableView.appearance.estimatedRowHeight = 0;
    UITableView.appearance.estimatedSectionFooterHeight = 0;
    UITableView.appearance.estimatedSectionHeaderHeight = 0;
    

    导航高的问题

    因为iOS11新增large Title和iPhone X的出现,导致以前导航高不是64了,然而很不幸的是,项目中充斥大量这种64的硬编码┑( ̄Д  ̄)┍
    笔者项目需求要求所有可滚动的控件统一采用充满屏幕然后设置contentInset,以达到内容可以穿越safe area的底部(项目相关bar已做透明度)

    productView!.mas_makeConstraints { (make) in
                make!.edges.equalTo()(self.view)?.width().setInsets(UIEdgeInsetsMake(0, 0, 0, 0))
            }
    productView?.contentInset = UIEdgeInsets(top: 40, left: 0, bottom: 0, right: 0)
            productView?.scrollIndicatorInsets = UIEdgeInsets(top: 40, left: 0, bottom: 0, right: 0)
    
    • 解决方案一
      设置相关宏
    导航高度
    static inline CGFloat regularTopGuideHeightWithExtras() {
        BOOL isIphoneX = (mScreenHeight == 812) ? YES : NO;
        if (isIphoneX) {
            return 88;
        }else{
            return 64;
        }
    }
    tabbar高度
    static inline CGFloat tabbarHeight() {
        BOOL isIphoneX = (mScreenHeight == 812) ? YES : NO;
        CGFloat tabBarHeight = isIphoneX ? 83 : 49;
        return tabBarHeight;
    }
    
    • 方案二
    static inline UIEdgeInsets sgm_safeAreaInset(UIView *view) {
        if (@available(iOS 11.0, *)) {
            return view.safeAreaInsets;
        }
        return UIEdgeInsetsZero;
    }
    static inline CGFloat topGuideHeightWithExtras(UIView *view, BOOL navbarHide, BOOL statusBarHide) {
        UIEdgeInsets inset = sgm_safeAreaInset(view);
        if (inset.top > 0) {
            return inset.top;
        }else{
            if (navbarHide && statusBarHide) {
                return 0;
            }else if (navbarHide) {
                return 20;
            }else if (statusBarHide) {
                return 44;
            }else{
                return 64;
            }
        }
    }
    

    然后每个需要的地方调用topGuideHeightWithExtras,采用这种方式要注意下safeAreaInsets的时机问题,如果view不在屏幕上或显示层级里,view的safeAreaInsets = UIEdgeInsetsZero,所以我们需要明确知道safeAreaInsets改变的时机

    对于UIViewController 在下面方法布局代码
    -(void) viewDidLayoutSubviews
    
    对于UIView 在下面代码布局
    - (void)safeAreaInsetsDidChange {
        [super safeAreaInsetsDidChange];
        UIEdgeInsets safeAreaInsets = sgm_safeAreaInset(self);
        CGFloat top = safeAreaInsets.top > 0 ? safeAreaInsets.top : STATUSBAR_HEIGHT; //
        self.skipButton.frame = CGRectMake(self.frame.size.width-70, top, 70, 35);
    }
    

    以上俩种笔者项目采用的第一种

    tabbar

    image.png

    如果是系统的tabbar是不会出现上述问题,系统已做好兼容,笔者项目是自定义的tababr,所以要重新适配下高度,其实就是iPhone X的时候增加34高度即可,以下判断iPhone X的宏,当然多种判断方法了

    * OC
    #define iPhoneX (mScreenHeight == 812) ? YES : NO
    
    * Swift
    struct ShareUtill {
        static func iPhoneX() -> Bool {
            if UIScreen.main.bounds.size.height == 812 {
                return true
            }
            return false
        }
    }
    获取tabbar 高度
    static inline CGFloat tabbarHeight() {
        BOOL iPhoneX = (mScreenHeight == 812) ? YES : NO;
        CGFloat tabBarHeight = iPhoneX ? 83 : 49;
        return tabBarHeight;
    }
    

    其它适配iPhone X的相应设计变动

    image.png

    项目很多类似这种设计的,所以见到一个改一个,那上面这种稍微好改些,根据官方的设计指南,做了一个简单的兼容
    效果如下


    image.png

    如果有时间的话,让你们的设计师妹纸适配下iPhone X的设计稿(就当没说这句话吧=。=

    启动图、新特性滚动图iPhone X适配

    如果启动图不是xib添加一张1125*2436.png图即可,滚动图根据实现方式亦然这样,笔者项目滚动图内容在iPhone X上被左右截部分,所以让设计师妹纸从新调整下内容文案的左右离屏的间距

    相册相机等相关权限的问题,需要在info.plist添加相应的key

    Masonry等三方库

    随着iOS11的发布,Masonry也相应做了升级,主要更新的是对safe area的支持,我们更新下本地pod即可,如果遇到搜索不到最新的版本,可以尝试pod repo update,如果各种慢可以参考:
    pod repo update
    最后附上官网相关指南
    Updating Your App for iOS 11
    Building Apps for iPhone X
    Designing for iPhone X
    Positioning Content Relative to the Safe Area

    相关文章

      网友评论

          本文标题:iOS11以及iPhone X适配

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