美文网首页iOS Developer
iOS11适配问题,你绝对要知道

iOS11适配问题,你绝对要知道

作者: 哇哦萌芽技术团队 | 来源:发表于2017-09-19 18:44 被阅读0次

最近手残下载XCode9 GM seed,跑了自己的项目,在iOS11 和 刘海机上跑了一下,发现了几个问题:
1、整个app所有的groupStyle 的 tableView cell之间的间隔变得奇大无比,tableView距顶部也偏移了一段距离
原因:iOS 11 后tableView做了比较多的变化,对自使用高度的支持更加深入了,tableView的sectionFooterHeight 以及 sectionHeaderHeight的默认高度是UITableViewAutomaticDimension,这使得旧项目跑起来cell之间的间隔会变宽,苹果这次iOS11的发布对低版本的兼容性也是没谁了。
解决办法:直接附代码

   if ([UIDevice currentDevice].systemVersion.floatValue >= 11.0)
    {
     tableView.sectionHeaderHeight = 0;
    tableView.sectionFooterHeight = 0;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
    tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
#endif
    }

在tableView初始化的时候加上这几句代码就可以了,sectionHeaderHeight以及sectionFooterHeight的设置解决了cell之间距离变大的问题,contentInsetAdjustmentBehavior的设置解决了tableView顶部偏移的问题
IPHONE_OS_VERSION_MAX_ALLOWED的预编译指令可以防止低版本xcode跑新接口时报错。

这时候问题又来了,我整个项目那么多个tableView,难道我要一个个在初始化的地方贴上上面的代码吗?那不是很蛋疼。后面我想了个相对优雅的办法,就是进行切面编程,hook一下tableView的初始化方法,这样就把所有的tableView直接在一个地方改过来了,具体创建实例时覆盖这几个属性也可以进行修改。下面直接附上代码

@implementation UITableView (GSHook)

+ (void)load
{
    /// 适配iOS11
    if ([UIDevice currentDevice].systemVersion.floatValue >= 11.0)
    {
         [self swizzleInstanceMethod:@selector(initWithFrame:style:) withMethod:@selector(gsinitWithFrame:style:) class:[UITableView class]];
    }
   
}

- (instancetype)gsinitWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    UITableView *tableView = [self gsinitWithFrame:frame style:style];
    tableView.sectionHeaderHeight = 0;
    tableView.sectionFooterHeight = 0;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
    tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
#endif
    return tableView;
    
}

+ (void)swizzleInstanceMethod:(SEL)origSelector withMethod:(SEL)newSelector class:(Class)cls
{
    /* if current class not exist selector, then get super*/
    Method originalMethod = class_getInstanceMethod(cls, origSelector);
    Method swizzledMethod = class_getInstanceMethod(cls, newSelector);
    /* add selector if not exist, implement append with method */
    if (class_addMethod(cls,
                        origSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod)) ) {
        /* replace class instance method, added if selector not exist */
        /* for class cluster , it always add new selector here */
        class_replaceMethod(cls,
                            newSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
        
    } else {
        /* swizzleMethod maybe belong to super */
        class_replaceMethod(cls,
                            newSelector,
                            class_replaceMethod(cls,
                                                origSelector,
                                                method_getImplementation(swizzledMethod),
                                                method_getTypeEncoding(swizzledMethod)),
                            method_getTypeEncoding(originalMethod));
    }
}

@end

2、页面自动向下偏移20个像素,“露出”状态栏
解决办法

 self.automaticallyAdjustsScrollViewInsets = NO;

设置控制器的这个属性就可以了

3、获取状态栏视图信息崩溃
这个问题是iphoneX上才会出现的问题,因为齐刘海的原因,状态栏一些UI元素无法获取,比如说statusBar属性发生了变化,UIStatusBar外面套了一层UIStatusBar_mordern类,各位老司机要注意一下这个问题了。

相关文章

网友评论

    本文标题:iOS11适配问题,你绝对要知道

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