在Xcode9以及ios11出來之后,在工作閑暇之余寫了點(diǎn)東西來測試一下WWDC更新的一些內(nèi)容,以下是我的一些分享,有問題或者疑惑的地方可以隨時(shí)溝通交流。
1.IOS 11的導(dǎo)航欄支持大標(biāo)題顯示
自動(dòng)設(shè)置,默認(rèn)是不顯示大標(biāo)題(適配沒有特殊需求是不需要處理的),當(dāng)視圖滾動(dòng)到上面時(shí)大標(biāo)題會(huì)消失,小標(biāo)題會(huì)顯示出來,跟原來的顯示效果一樣
// self.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
// UINavigationItemLargeTitleDisplayModeAutomatic
// 大標(biāo)題
// UINavigationItemLargeTitleDisplayModeAlways
// 小標(biāo)題
// UINavigationItemLargeTitleDisplayModeNever
2.搜索框的添加
當(dāng)設(shè)置問搜索框后,TableView向上滑動(dòng)時(shí)搜索框就會(huì)出現(xiàn)
self.navigationItem.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
3.UITableView左滑刪除效果添加新方法
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"刪除";
}
// 第一種方法 這個(gè)是以前我們用的方法
//*8.0之后出現(xiàn)的可以設(shè)置左劃后顯示的內(nèi)容
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:1 title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
if ([_dataList count]>indexPath.row) {
[_dataList removeObjectAtIndex:indexPath.row];
[_tableView reloadData];
}
}];
action1.backgroundColor = [UIColor redColor];
return @[action1];
}
// 第二種 ios 11以后新增方法
- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
//刪除
UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
if ([_dataList count]>indexPath.row) {
[_dataList removeObjectAtIndex:indexPath.row];
[_tableView reloadData];
}
completionHandler (YES);
}];
deleteRowAction.image = [UIImage imageNamed:@"icon_del"];
deleteRowAction.backgroundColor = [UIColor blueColor];
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
return config;
}
Xcode9下相冊等訪問權(quán)限問題
之前項(xiàng)目中相機(jī)功能一直使用系統(tǒng)自帶的PickerView,說實(shí)話不甚美觀,自己空閑之余一直著手開發(fā)自定義相機(jī)(EVNCamera:給個(gè)StarO(∩_∩)O~)。在Xcode9的首個(gè)Beta版本中開發(fā)相機(jī)功能時(shí)發(fā)現(xiàn),原有項(xiàng)目竟然crash,后來發(fā)現(xiàn)iOS11下,蘋果對相冊的權(quán)限key做了調(diào)整,原來的 NSPhotoLibraryUsageDescription ,在iOS11之后,改成了NSPhotoLibraryAddUsageDescription。詳見:Cocoa Keys
?:不過有童鞋反饋使用Xcode 9 Beta3中打包應(yīng)用,使用原有相冊權(quán)限NSPhotoLibraryUsageDescription依舊正常,本人嘗試Xcode 9 Beta4中打包,使用原來相冊權(quán)限的key依舊crash。
近場通訊NFC權(quán)限
在iOS11中,蘋果開放了NFC(Near field communication),怕也是其推廣ApplePay的一種策略。在使用近場通訊時(shí),首先也要在info.plist配置NFCReaderUsageDescription 權(quán)限,案例步驟,如下:
iOS 11 Core NFC - any sample code?
以上幾點(diǎn)改動(dòng)效果圖如下圖所示:
Demo下載地址:(https://github.com/FycGitHub/IOS11AdapterDemo/tree/master)
下面介紹以下Xcode9 IOS11 在iphoneX上TableView和UIScrollView上面的bug,頭部刷新UI出現(xiàn)了錯(cuò)亂,查閱發(fā)現(xiàn) iOS11棄用了automaticallyAdjustsScrollViewInsets屬性,新增contentInsetAdjustmentBehavior來替代它
關(guān)于 contentInsetAdjustmentBehavior
1.UITableView上面用的SVPullToRefresh下拉刷新顯示多出一條,問題如下圖所示:
2.UIScrollView上面也多出一個(gè)空白條,問題如下圖所示:
這兩個(gè)問題統(tǒng)一的解決方法是:分別封裝底層的UITableView和UIScrollView 子類繼承該類,并實(shí)現(xiàn)該方法:
if (@available(iOS 11.0, *)) {
self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
}
最終修改后效果如下所示:
以下關(guān)于ios11其他方面寫的比較不錯(cuò)文章推薦給大家,作者是個(gè)
騰訊Bugly: https://mp.weixin.qq.com/s/AZFrqL9dnlgA6Vt2sVhxIw
404你懂得:http://www.lxweimin.com/u/ffa5cb2a5d4e
404你懂得:http://www.lxweimin.com/p/370d82ba3939