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 計算內邊距