您的位置:首页 > 移动开发

APP适配 iOS 11和iPhone X总结

2017-09-25 12:28 926 查看

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

iOS 11中如果不实现-tableView: viewForFooterInSection: 和 -tableView: viewForHeaderInSection:,
那么-tableView: heightForHeaderInSection:和- tableView: heightForFooterInSection:不会被调用。

这是因为estimatedRowHeight estimatedSectionHeaderHeight estimatedSectionFooterHeight三个高度估算属性
由默认的0变成了UITableViewAutomaticDimension,导致高度计算不对,解决方法是实现对应方法或吧这三个属性设为0。

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

  
如果使用宏定义封装可以使用下面的方法:  
宏定义为:  
#define adjustsScrollViewInsets_NO(scrollView,vc) [Factory adjustsScrollViewInsets:scrollView andVc:vc]  
  
定义一个类方法:  
  
+(void) adjustsScrollViewInsets:(UIScrollView *)scrollView andVc:(UIViewController *)VC;  
  
实现该类方法:  
  
+(void) adjustsScrollViewInsets:(UIScrollView *)scrollView andVc:(UIViewController *)VC{  
      
    if (@available(iOS 11.0, *)) {  
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;  
    } else {  
        VC.automaticallyAdjustsScrollViewInsets = NO;  
    }  
}  
  
实现调用就一句话:  
adjustsScrollViewInsets_NO(self.tableView,self); 

3. 导航栏按钮位置问题

UIBarButtonItem:描述按钮具体的内容

UIButton包装成UIBarButtonItem就会导致点击区域扩大



应该包装成UIVIew在进行添加





代码如下:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
 [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
 [btn setImage:[UIImage imageNamed:highImage] forSt ate:UIControlStateHighlighted];
 [btn sizeToFit];
 [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIView *containVew = [[UIView alloc] initWithFrame:btn.bounds];
[containVew addSubview:btn];
 return [[UIBarButtonItem alloc]initWithCustomView:containVew];

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

//调整按钮边距
//    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像素问题不大,其他请自行测试,若有更完美的办法请联系我更新。

4. 位置权限

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

快去更新你的info.plist

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

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

……

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

该文章引用了部分内容,加上自己总结的进行完善。

引用的文章地址:http://www.jianshu.com/p/94d3fdc0f20d





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: