scrollView 在控制器中内间距适配

iOS 11之前,控制器使用automaticallyAdjustsScrollViewInsets属性控制scrollView的内边距,以自动适配scrollView不被导航栏,状态栏等遮挡;但是该属性从iOS 11开始被废弃

@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets 
API_DEPRECATED_WITH_REPLACEMENT("Use UIScrollView's contentInsetAdjustmentBehavior instead", ios(7.0,11.0),tvos(7.0,11.0)); // Defaults to YES

在iOS 11中,UIScrollView新增了如下属性以代替automaticallyAdjustsScrollViewInsets

@property(nonatomic) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior 
API_AVAILABLE(ios(11.0),tvos(11.0));

typedef enum UIScrollViewContentInsetAdjustmentBehavior : NSInteger {
    // Automatically adjust the scroll view insets.
    UIScrollViewContentInsetAdjustmentAutomatic, 
    // Adjust the insets only in the scrollable directions.
    // 只在能够滚动的方向上适配内间距
    UIScrollViewContentInsetAdjustmentScrollableAxes, 
    // Do not adjust the scroll view insets.
    UIScrollViewContentInsetAdjustmentNever, 
    // Always include the safe area insets in the content adjustment.
    // 总是适配,并且包含安全区域部分
    UIScrollViewContentInsetAdjustmentAlways 
} UIScrollViewContentInsetAdjustmentBehavior;

参考:

适配iOS11--contentInsetAdjustmentBehavior


UITableView 适配iOS 11 设置

+ (UITableView *)tableViewOfGroupedAndSetDelegate:(id)delegate {
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero
                                                          style:UITableViewStyleGrouped];
    tableView.delegate = delegate;
    tableView.dataSource = delegate;
    tableView.backgroundColor = LGColorBackgroundColorGray;
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;

    // 适配iOS11:两项必须设置
    tableView.sectionHeaderHeight = 0;
    tableView.sectionFooterHeight = 0;

    if (@available(iOS 11.0, *)) {
        // 发现第一个 section 顶部会多出一部分的距离(tableHeaderView 占据的空间,即使没有设置)
        // 设置一个高度很小的 tableHeaderView ,注意:高度不能设置为 0,否则不起作用
        tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        tableView.estimatedSectionHeaderHeight = 0.f;
    }

    return tableView;
}

注意:

UITableView为 UITableViewStyleGrouped 类型时,必须需要以下方法方法返回真实高度,如果是 0,使用 CGFLOAT_MIN

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
 return CGFLOAT_MIN;
 }
 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
 return CGFLOAT_MIN;
 }

results matching ""

    No results matching ""