美文网首页UI
iOS 11适配(UITableView篇)

iOS 11适配(UITableView篇)

作者: Poseidong | 来源:发表于2018-01-09 10:51 被阅读22次

    首先介绍几个属性:
    @property(nonatomic) BOOL viewRespectsSystemMinimumLayoutMargins

    • UIViewController 属性. 默认为 YES. 视图遵守系统最小 layoutMargins
      YES: 视图 layoutMarginsdirectionalLayoutMargins 的返回值不小于系统最小值(systemMinimumLayoutMargins).
      NO: layoutMargins 为完全可定制的.
      所以要在 UIViewController 中添加如下代码:
    if (@available(iOS 11.0, *)) {
        self.viewRespectsSystemMinimumLayoutMargins = NO;
    }
    

    @property (nonatomic) UIEdgeInsets layoutMargins
    @property (nonatomic) NSDirectionalEdgeInsets directionalLayoutMargins

    • UIView 属性. 设置视图边缘. layoutMargins 为iOS8后可用. directionalLayoutMargins 为iOS11可用, 相当于 layoutMargins 的升级版. iOS11后, 建议使用 directionalLayoutMargins.

    下面是如何实现:

    • 核心代码:
      tableView.layoutMargins = UIEdgeInsetsZero;
      或者
      tableView.directionalLayoutMargins = NSDirectionalEdgeInsetsZero;

    但是如果为每个 UITabelView 都修改该属性, 工作量会很大. 所以, 采用创建UITabelView 的 category 方法, 利用 runtime 在初始化的时候修改该属性, 便可达到适配的目的.

    代码实现:

    #import "UITableView+Fix.h"
    #import <objc/runtime.h>
    
    @implementation UITableView (Fix)
    
    + (void)load
    {
        //交换实现
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 11.0) {
            //代码创建(调用initWithFrame:style:方法)
            Method sysMethod = class_getInstanceMethod([self class], @selector(initWithFrame:style:));
            Method fixMethod = class_getInstanceMethod([self class], @selector(fix_initWithFrame:style:));
            method_exchangeImplementations(sysMethod, fixMethod);
            //xib或sb创建(调用initWithCoder:方法)
            Method xSysMethod = class_getInstanceMethod([self class], @selector(initWithCoder:));
            Method xFixMethod = class_getInstanceMethod([self class], @selector(fix_initWithCoder:));
            method_exchangeImplementations(xSysMethod, xFixMethod);
        }
    }
    
    - (instancetype)fix_initWithCoder:(NSCoder *)aDecoder
    {
        UITableView *tableView = [self fix_initWithCoder:aDecoder];
        
        [self fixTableView:tableView];
        
        return tableView;
    }
    
    - (instancetype)fix_initWithFrame:(CGRect)frame style:(UITableViewStyle)style
    {
        UITableView *tableView = [self fix_initWithFrame:frame style:style];
        
        [self fixTableView:tableView];
        
        return tableView;
    }
    
    - (void)fixTableView:(UITableView *)tableView
    {
        /**
         如果目前项目中没有使用estimateRowHeight属性,在iOS11的环境下就要注意了,因为开启Self-Sizing之后,
         tableView是使用estimateRowHeight属性的,这样就会造成contentSize和contentOffset值的变化,
         如果是有动画是观察这两个属性的变化进行的,就会造成动画的异常,
         因为在估算行高机制下,contentSize的值是一点点地变化更新的,所有cell显示完后才是最终的contentSize值。
         因为不会缓存正确的行高,tableView reloadData的时候,会重新计算contentSize,就有可能会引起contentOffset的变化
         
         简言之,如果项目中没有使用estimateRowHeight属性,就添加下面的代码,不使用Self-Sizing,
         不然会因为contentOffset的变化,而引起一直加载的BUG
         */
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        //处理tableView左右间距的BUG
        if (@available(iOS 11.0, *)) {
            tableView.directionalLayoutMargins = NSDirectionalEdgeInsetsZero;
            /**
             如果为 UITableViewStyleGrouped ,在UITableView设置代理之前调用directionalLayoutMargins
             顶部会出现40 point的 tableHeaderView, 所以在调用directionalLayoutMargins之后,
             设置一个高度非常小的 tableHeaderView (如果高度为0, 还是会出现40 point的 tableHeaderView)
             */
            if (tableView.style == UITableViewStyleGrouped) {
                tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
            }
        }
    }
    @end
    
    

    相关文章

      网友评论

        本文标题:iOS 11适配(UITableView篇)

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