iOS 11系統(tǒng)中谓着,導(dǎo)航欄新加入了LargeTitles和searchController兩個(gè)新特性唉俗,UITableView新增了滑動(dòng)操作(Swipe Actions)代理,本文主要介紹這兩方面。
特性一:導(dǎo)航欄
1末融、設(shè)置導(dǎo)航欄大標(biāo)題
self.navigationController.navigationBar.prefersLargeTitles = YES;
2节仿、設(shè)置導(dǎo)航欄搜索控制器
iOS 11中晤锥,NavigationItem
增加了新的屬性searchController
,只需實(shí)例一個(gè)UISearchController
廊宪,之后設(shè)置為navigationItem.searchController
即可矾瘾。
具體實(shí)現(xiàn)如下:
_resultVC = [[ResultViewController alloc] init];
UISearchController *search = [[UISearchController alloc] initWithSearchResultsController:_resultVC];
search.searchResultsUpdater = self;
search.delegate = self;
search.hidesNavigationBarDuringPresentation = NO;
self.navigationItem.searchController = search;
此處將searchResultsController設(shè)置為其他的控制器,去實(shí)現(xiàn)自己的邏輯箭启。
注:需要注意的是將當(dāng)前控制器的definesPresentationContext
屬性設(shè)為true
壕翩,否則在切換searchResultsController時(shí)整個(gè)導(dǎo)航欄會(huì)消失,即:
self.definesPresentationContext = YES;
其中search.searchResultsUpdater = self;
代理方法- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
中傅寡,處理搜索邏輯放妈。
其中search.hidesNavigationBarDuringPresentation = NO;
搜索時(shí)是否隱藏導(dǎo)航欄北救。
簡(jiǎn)單Demo
特性二:滑動(dòng)操作(Swipe Actions)
1、iOS 8中UITableVIew的右滑操作接口
在iOS 8之后芜抒,蘋果官方增加了UITableVIew的右滑操作接口珍策,即新增了一個(gè)代理方法tableView: editActionsForRowAtIndexPath:
和一個(gè)類UITableViewRowAction
,代理方法返回的是一個(gè)數(shù)組宅倒,我們可以在這個(gè)代理方法中定義所需要的操作按鈕(刪除攘宙、置頂?shù)?,這些按鈕的類就是UITableViewRowAction
唉堪。只能定義按鈕的顯示文字模聋、背景色、按鈕事件唠亚。
2链方、iOS 11中UITableVIew新增代理方法,支持按鈕設(shè)置圖片灶搜,支持左滑祟蚀、右滑操作
首先相對(duì)iOS 8的方法,支持設(shè)置按鈕圖片啦割卖。
代理方法的替換如下:
// Swipe actions
// These methods supersede -editActionsForRowAtIndexPath: if implemented
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath前酿;
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath;
這兩個(gè)代理方法返回的是UISwipeActionsConfiguration
類型的對(duì)象鹏溯,創(chuàng)建該對(duì)象及賦值 代碼片段如下:
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
//刪除按鈕(系統(tǒng)類型)
UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
[self.titleArr removeObjectAtIndex:indexPath.row];
completionHandler(YES);
}];
//設(shè)置圖片
deleteRowAction.image = [UIImage imageNamed:@"icon_del"];
//UISwipeActionsConfiguration類
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:
@[deleteRowAction]];
return config;
}
創(chuàng)建UIContextualAction
對(duì)象時(shí)罢维,UIContextualActionStyle
有兩種類型:
typedef NS_ENUM(NSInteger, UIContextualActionStyle) {
UIContextualActionStyleNormal,
UIContextualActionStyleDestructive
} NS_SWIFT_NAME(UIContextualAction.Style)
如果是置頂、已讀等按鈕就使用UIContextualActionStyleNormal
類型丙挽。delete按鈕可使用UIContextualActionStyleDestructive
類型肺孵,當(dāng)使用該類型時(shí),如果是右滑操作颜阐,一直向右滑動(dòng)某個(gè)cell平窘,會(huì)直接執(zhí)行刪除操作??。
總結(jié):
介紹了iOS 11中一些新特性凳怨,未完待續(xù)... ...