适配 iOS 11 & iPhone X

来源:互联网 发布:联想数据恢复中心 编辑:程序博客网 时间:2024/05/16 13:11

适配中的问题及解决办法

1. 滚动条高度跳动、上下拉刷新问题:

self.tableView.estimatedRowHeight = 0;self.tableView.estimatedSectionHeaderHeight = 0;self.tableView.estimatedSectionFooterHeight = 0;

2. 列表/页面偏移

本来是这样的

if (@available(iOS 11.0, *)){        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;    }

目前发现所有的Scrollview 及其子类都需要设置 contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever ,工程中大量使用列表的同学不要慌,不要忙,因为UIView及其子类都遵循UIAppearance协议,我们可以进行全局配置:

// AppDelegate 进行全局设置    if (@available(iOS 11.0, *)){        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];    }

这样一来使用UITableview 、UICollectionView、UIScrollview的时候就不需要再单独设置该属性了。

3. 导航栏按钮位置问题

之前这样写控制按钮的边距

    //调整按钮边距//    UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];//    //将宽度设为负值//    spaceItem.width= -5;//    [items addObject:spaceItem];

今日不同往日,此方法无效了。
我试着使用了下面的方法

#pragma mark ————— 导航栏 添加文字按钮 —————- (NSMutableArray<UIButton *> *)addNavigationItemWithTitles:(NSArray *)titles isLeft:(BOOL)isLeft target:(id)target action:(SEL)action tags:(NSArray *)tags{        NSMutableArray * items = [[NSMutableArray alloc] init];        //调整按钮位置//    UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];//    //将宽度设为负值//    spaceItem.width= -5;//    [items addObject:spaceItem];        NSMutableArray * buttonArray = [NSMutableArray array];    NSInteger i = 0;    for (NSString * title in titles) {        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];        btn.frame = CGRectMake(0, 0, 30, 30);        [btn setTitle:title forState:UIControlStateNormal];        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];        btn.titleLabel.font = SYSTEMFONT(16);        [btn setTitleColor:KWhiteColor forState:UIControlStateNormal];        btn.tag = [tags[i++] integerValue];        [btn sizeToFit];                //设置偏移        if (isLeft) {            [btn setContentEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];        }else{            [btn setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, -10)];        }                UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:btn];        [items addObject:item];        [buttonArray addObject:btn];    }    if (isLeft) {        self.navigationItem.leftBarButtonItems = items;    } else {        self.navigationItem.rightBarButtonItems = items;    }    return buttonArray;}

图层调试发现此法其实属障眼法,并不完美,设置内容偏移,其实际位置并没有发生变化,这可能导致按钮部分区域无法点击,目前偏移10像素问题不大,其他请自行测试,若有更完美的办法请联系我更新。

4. 位置权限

在IOS11,原有的NSLocationAlwaysUsageDeion被降级为NSLocationWhenInUseUsageDeion。因此,在原来项目中使用requestAlwaysAuthorization获取定位权限,而未在plist文件中配置NSLocationAlwaysAndWhenInUseUsageDeion,系统框不会弹出。建议新旧key值都在plist里配置,反正我试下来是没有问题,唯一的区别是使用requestAlwaysAuthorization获取权限 IOS11系统弹框会把几种权限级别全部列出,供用户选择,显然更人性化了。
快去更新你的info.plist

    <!-- 位置 -->    <key>NSLocationUsageDescription</key>    <string>获取地理位置,精准推送服务</string>    <!-- 在使用期间访问位置 -->    <key>NSLocationWhenInUseUsageDescription</key>    <string>获取地理位置,精准推送服务</string>    <!-- 始终访问位置 -->    <key>NSLocationAlwaysUsageDescription</key>    <string>App需要您的同意,才能始终访问位置</string>    <!-- iOS 11访问位置 -->    <key>NSLocationAlwaysAndWhenInUseUsageDeion</key>    <string>App需要您的同意,才能始终访问位置</string>

5. iPhone X 适配

iPhone X 变化最大的是头部 & 底部
非iPhone X :
StatusBar 高20px,NavigationBar 高44px,底部TabBar高49px
iPhone X:
StatusBar 高44px,NavigationBar 高44px,底部TabBar高83px
所以,之前项目里写死的 ±49 ±64 都要出问题,如果你之前抽离出来使用的是宏,那问题不大,如果不是,开始搬砖吧少年。
送你几个宏,来日好好撸,莫偷懒

#define kStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height#define kNavBarHeight 44.0#define kTabBarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?83:49)#define kTopHeight (kStatusBarHeight + kNavBarHeight)

替换 64px →kTopHeight
替换 49px →kTabBarHeight

6. iPhone X push的时候TabBar上移

答案在这:适配iPhone X Push过程中TabBar位置上移

这样可以解决大部分因位置导致的适配问题。