iOS 11开发新特性

iOS 11系统中,导航栏新加入了LargeTitles和searchController两个新特性,UITableView新增了滑动操作(Swipe Actions)代理,本文主要介绍这两方面。

特性一:导航栏

iOS 11开发新特性_第1张图片
图 1.png
1、设置导航栏大标题
self.navigationController.navigationBar.prefersLargeTitles = YES;
2、设置导航栏搜索控制器

iOS 11中,NavigationItem增加了新的属性searchController,只需实例一个UISearchController,之后设置为navigationItem.searchController即可。
具体实现如下:

_resultVC = [[ResultViewController alloc] init];
UISearchController *search = [[UISearchController alloc] initWithSearchResultsController:_resultVC];
search.searchResultsUpdater = self;
search.delegate = self;
search.hidesNavigationBarDuringPresentation = NO;
self.navigationItem.searchController = search;

此处将searchResultsController设置为其他的控制器,去实现自己的逻辑。
注:需要注意的是将当前控制器的definesPresentationContext属性设为true,否则在切换searchResultsController时整个导航栏会消失,即:

self.definesPresentationContext = YES;

其中search.searchResultsUpdater = self;代理方法- (void)updateSearchResultsForSearchController:(UISearchController *)searchController中,处理搜索逻辑。
其中search.hidesNavigationBarDuringPresentation = NO;搜索时是否隐藏导航栏。
简单Demo

特性二:滑动操作(Swipe Actions)

1、iOS 8中UITableVIew的右滑操作接口

在iOS 8之后,苹果官方增加了UITableVIew的右滑操作接口,即新增了一个代理方法tableView: editActionsForRowAtIndexPath:和一个类UITableViewRowAction,代理方法返回的是一个数组,我们可以在这个代理方法中定义所需要的操作按钮(删除、置顶等),这些按钮的类就是UITableViewRowAction。只能定义按钮的显示文字、背景色、按钮事件。

2、iOS 11中UITableVIew新增代理方法,支持按钮设置图片,支持左滑、右滑操作

首先相对iOS 8的方法,支持设置按钮图片啦。
代理方法的替换如下:

// Swipe actions
// These methods supersede -editActionsForRowAtIndexPath: if implemented
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath;
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath;

这两个代理方法返回的是UISwipeActionsConfiguration类型的对象,创建该对象及赋值 代码片段如下:

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    //删除按钮(系统类型)
    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);
    }];
    //设置图片
    deleteRowAction.image = [UIImage imageNamed:@"icon_del"];
    //UISwipeActionsConfiguration类
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:
    @[deleteRowAction]];
    return config;
}

创建UIContextualAction对象时,UIContextualActionStyle有两种类型:

typedef NS_ENUM(NSInteger, UIContextualActionStyle) {
    UIContextualActionStyleNormal,
    UIContextualActionStyleDestructive
} NS_SWIFT_NAME(UIContextualAction.Style)

如果是置顶、已读等按钮就使用UIContextualActionStyleNormal类型。delete按钮可使用UIContextualActionStyleDestructive类型,当使用该类型时,如果是右滑操作,一直向右滑动某个cell,会直接执行删除操作。

总结:

介绍了iOS 11中一些新特性,未完待续... ...

你可能感兴趣的:(iOS 11开发新特性)