美文网首页
iOS 11 和Xcode9适配

iOS 11 和Xcode9适配

作者: 我的名字就这么长 | 来源:发表于2017-10-30 11:34 被阅读56次

    iOS 11 和Xcode9适配

    Xcode9下相册等访问权限问题

    iOS11苹果对相册的权限key做了调整,原来的 NSPhotoLibraryUsageDescription ,在iOS11之后,改成了NSPhotoLibraryAddUsageDescription。

    不过有童鞋反馈使用Xcode 9 Beta3中打包应用,使用原有相册权限NSPhotoLibraryUsageDescription依旧正常,本人尝试Xcode 9 Beta4中打包,使用原来相册权限的key依旧crash。

    近场通讯NFC权限

    在iOS11中,苹果开放了NFC(Near field communication),怕也是其推广ApplePay的一种策略。

    在使用近场通讯时,首先也要在info.plist配置NFCReaderUsageDescription 权限,案例步骤,如下:

    iOS 11 Core NFC - any sample code?

    iOS11需要适配的地方

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 不执行

    一:UITableView:默认开启Self-Sizing,大概就是说我们不再需要自己去计算cell的高度了,只要设置好这两个属性,约束好布局,系统会自动计算好cell的高度。iOS11以后,Self-Sizing默认开启,包括Headers, footers。如果项目中没使用estimatedRowHeight属性,在iOS11下会有奇奇怪怪的现象,因为iOS11之前,estimatedRowHeight默认为0,Self-Sizing自动打开后,contentSize和contentOffset都可能发生改变。可以通过以下方式禁用:

    self.tableView.estimatedRowHeight = 0;

    self.tableView.estimatedSectionHeaderHeight = 0;

    self.tableView.estimatedSectionFooterHeight = 0;

    automaticallyAdjustsScrollViewInsets 被废弃,TabView,CollectionView间距问题

    解决方案

    automaticallyAdjustsScrollViewInsets属性已经不再使用,我们需要使用UIScrollView的

    contentInsetAdjustmentBehavior

    属性来替代它.

    设置适当的枚举:

    if(@available(iOS11.0,*)){

    self.tableView.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;

    }

    else{

    self.automaticallyAdjustsScrollViewInsets=NO;

    }

    NSLocationAlwaysAndWhenInUseUsageDeion

    在iOS11,原有的NSLocationAlwaysUsageDeion被降级为NSLocationWhenInUseUsageDeion。因此,在原来项目中使用requestAlwaysAuthorization获取定位权限,而未在plist文件中配置NSLocationAlwaysAndWhenInUseUsageDeion,系统框不会弹出。

    iPhone X状态栏图标元素结构变了

    我们之前通过遍历foregroundView,UIStatusBarDataNetworkItemView可以找到wifi信号强度。

    -(void)getSignalStrength{

    UIApplication*app=[UIApplicationsharedApplication];

    NSArray*subviews=[[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];

    NSString*dataNetworkItemView=nil;

    for(idsubview insubviews){

    if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView")class]]){

    dataNetworkItemView=subview;

    break;

    }

    }

    intsignalStrength=[[dataNetworkItemView valueForKey:@"_wifiStrengthBars"] intValue];

    NSLog(@"signal %d",signalStrength);

    }

    由于iPhoneX的留海设计,所以元素与布局都发现了变化

    idstatusBar=[[UIApplicationsharedApplication] valueForKeyPath:@"statusBar"];

    断点后执行

    po[statusBar recursiveDescription]

    即可查看新的结构

    二:navigation bar

    1、导航栏新增了一种大标题样式,默认设置是不开启,所以不需要修改。

    2、titleView支持autolayout,这要求titleView必须是能够自撑开的或实现了- intrinsicContentSize

    解决方案

    -(CGSize)intrinsicContentSize{

    returnUILayoutFittingExpandedSize;

    }

    三:ScrollView

    如果有一些文本位于UI滚动视图的内部,并包含在导航控制器中,现在一般navigationContollers会传入一个contentInset给其最顶层的viewController的scrollView,在iOS11中进行了一个很大的改变,不再通过scrollView的contentInset属性了,而是新增了一个属性:adjustedContentInset

    新增的contentInsetAdjustmentBehavior属性用来配置adjustedContentInset的行为,该结构体有以下几种类型:

    typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {

    UIScrollViewContentInsetAdjustmentAutomatic,

    UIScrollViewContentInsetAdjustmentScrollableAxes,

    UIScrollViewContentInsetAdjustmentNever,

    UIScrollViewContentInsetAdjustmentAlways,

    }

    @property(nonatomic) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior;

    @property(nonatomic, readonly) UIEdgeInsets adjustedContentInset;

    //adjustedContentInset值被改变的delegate

    - (void)adjustedContentInsetDidChange;

    - (void)scrollViewDidChangeAdjustedContentInset:(UIScrollView *)scrollView;

    UIScrollViewContentInsetAdjustmentBehavior 是一个枚举类型,值有以下几种:

    automatic 和scrollableAxes一样,scrollView会自动计算和适应顶部和底部的内边距并且在scrollView 不可滚动时,也会设置内边距.

    scrollableAxes 自动计算内边距.

    never不计算内边距

    always 根据safeAreaInsets 计算内边距

    相关文章

      网友评论

          本文标题:iOS 11 和Xcode9适配

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