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

iOS11及iPhone X适配

作者: 苏黎有个世 | 来源:发表于2017-09-22 09:58 被阅读529次

一、UITableView问题

升级iOS11后发现没写行高的地方行高变很小,section间距莫名变大

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

上面的问题经测试,我认为解决方法最好把sectionHeaderHeight和sectionFooterHeight属性设为0,rowHeight设为默认的44或estimatedRowHeight设为0。
tbView.rowHeight = 44;
tbView.sectionFooterHeight = 0;
tbView.sectionHeaderHeight = 0;

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

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
由于automaticallyAdjustsScrollViewInsets在iOS11中被废弃,可采用如下方法
if (@available(iOS 11.0, *)) {
    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
    self.automaticallyAdjustsScrollViewInsets = NO;
}
但是以上代码会在Xcode 9以下版本报错,故我在代码中这样处理
SEL selector = NSSelectorFromString(@"setContentInsetAdjustmentBehavior:");

if ([tbView respondsToSelector:selector]) {
    
    IMP imp = [tbView methodForSelector:selector];
    void (*func)(id, SEL, NSInteger) = (void *)imp;
    func(tbView, selector, 2);
    
    // [tbView performSelector:@selector(setContentInsetAdjustmentBehavior:) withObject:@(2)];
}

附上我创建的grouped样式tableView代码

- (JKBaseTableView *)tableViewGrouped{
    if (!_tableViewGrouped) {
        JKBaseTableView *tbView = [[JKBaseTableView alloc] initWithFrame:CGRectMake(0, JKNavBarHeight, self.view.width, self.view.height - JKNavBarHeight) style:(UITableViewStyleGrouped)];
        tbView.backgroundColor = JKGlobalBgColor;
        tbView.dataSource = self;
        tbView.delegate = self;
        [self.view insertSubview:tbView atIndex:0];
        
        tbView.rowHeight = 44;
        tbView.sectionFooterHeight = 0;
        tbView.sectionHeaderHeight = 0;
        
        tbView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JKScreenW, CGFLOAT_MIN)];
        tbView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JKScreenW, CGFLOAT_MIN)];
        
        //        if (@available(iOS 11.0, *)) {
        //            tbView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        //        }
        
        SEL selector = NSSelectorFromString(@"setContentInsetAdjustmentBehavior:");
        
        if ([tbView respondsToSelector:selector]) {
            
            IMP imp = [tbView methodForSelector:selector];
            void (*func)(id, SEL, NSInteger) = (void *)imp;
            func(tbView, selector, 2);
            
            // [tbView performSelector:@selector(setContentInsetAdjustmentBehavior:) withObject:@(2)];
        }
        
        tbView.scrollsToTop = YES;
        tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        // 约束tbView
        tbView.translatesAutoresizingMaskIntoConstraints = NO;
        [tbView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.top.mas_equalTo(JKNavBarHeight);
        }];
        
        _tableViewGrouped = tbView;
    }
    return _tableViewGrouped;
}

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

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

二、导航条问题

先贴出参考文章的说明

1、导航栏新增了一种大标题样式,默认设置是不开启,所以不需要修改。
2、titleView支持autolayout,这要求titleView必须是能够自撑开的或实现了- intrinsicContentSize解决办法比较简单,这个搜索框对应的view实现- intrinsicContentSize方法

- (CGSize)intrinsicContentSize {
    return UILayoutFittingExpandedSize;
}
我的解决办法

导航条上加搜索框是很多APP都带有的,我之前写的就是将搜索框设置为titleView。
但titleView在iOS11中无法向两边延伸。如下图:

搜索框

参考文章中提到的intrinsicContentSize我暂时还不知道该怎么用,所以我首先尝试了对titleView进行约束,约束确实有效果,但是在push/pop的时候每次都需要重新约束。
所以简单粗暴一点,顶部自定义一个view放搜索框,在当前控制器中隐藏navigationBar,其它地方显示。
当然,我已经将navigationBar设置为完全透明的颜色了,否则还会有其它问题。

三、iPhone X问题

1. 屏幕尺寸375 x 812
2. 状态栏高度为44pt
3. 底部工具栏要为home indicator留出34pt的边距
4. 屏幕有圆角,注意至少留出10pt边距
5. 启动图1125x2436  3x
5. AppIcon 1024x1024  1x
之前我都是把状态栏高度、导航条高度以及tabBar高度写成常量。为适配iPhone X,将它们改为宏定义
#define JKIsIphoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
#define JKStatusBarHeight (JKIsIphoneX ? 44 : 20)
#define JKNavBarHeight (JKStatusBarHeight + 44)
#define JKTabBarHeight (JKIsIphoneX ? 49.f+34.f : 49.f)
#define JKCurrentHomeIndicatorHeight (JKIsIphoneX ? 34 : 0)

参考:http://www.jianshu.com/p/26fc39135c34

相关文章

网友评论

    本文标题:iOS11及iPhone X适配

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