美文网首页
适配iPhone X 和 iOS11

适配iPhone X 和 iOS11

作者: Skysama | 来源:发表于2017-09-20 11:29 被阅读0次

1.启动图的适配
用xcode9打开原来的项目使用会出现项目不能完全契合的情况 原因是没有采用xib 或者xib 进行适配的
解决方法 在


99015CD2-3C12-4651-8C90-D9B7487F9379.png

设置下 Launch Screen File 之后是这样的


2D4587CB-70F2-4684-A863-AA505E08E1FF.png

适配中的问题及解决办法

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

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

  1. 列表/页面偏移

本来是这样的

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的时候就不需要再单独设置该属性了。

  1. 导航栏按钮位置问题

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

//调整按钮边距

// UIBarButtonItem* spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
// //将宽度设为负值
// spaceItem.width= -5;
// [items addObject:spaceItem];
今日不同往日,此方法无效了。

我试着使用了下面的方法

pragma mark ————— 导航栏 添加文字按钮 —————

  • (NSMutableArray *)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像素问题不大,其他请自行测试,若有更完美的办法请联系我更新。

  1. 位置权限

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

快去更新你的info.plist

NSLocationUsageDescription
获取地理位置,精准推送服务
NSLocationWhenInUseUsageDescription
获取地理位置,精准推送服务
NSLocationAlwaysUsageDescription
App需要您的同意,才能始终访问位置
NSLocationAlwaysAndWhenInUseUsageDeion
App需要您的同意,才能始终访问位置

  1. 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

……

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

相关文章

网友评论

      本文标题:适配iPhone X 和 iOS11

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