iOS 11适配之UIScrollView 和 UITableView

挺久没有写博客了,去年年末忙于新需求的开发和一些已有页面的 iOS 11适配以及 iPhone X 适配,年初刚好不会太忙,抽点时间写点博客。

ScrollView

苹果在iOS11中新增了一个属性:adjustedContentInset,新增的contentInsetAdjustmentBehavior属性用来配置adjustedContentInset,其结构体类型如下:

typedefNS_ENUM(NSInteger,UIScrollViewContentInsetAdjustmentBehavior) {
UIScrollViewContentInsetAdjustmentAutomatic,
UIScrollViewContentInsetAdjustmentScrollableAxes,
UIScrollViewContentInsetAdjustmentNever,
UIScrollViewContentInsetAdjustmentAlways,
}

@property(nonatomic)UIScrollViewContentInsetAdjustmentBehavior
contentInsetAdjustmentBehavior;
@property(nonatomic,readonly)UIEdgeInsets
adjustedContentInset;

// adjustedContentInset值改变的delegate
- (void)adjustedContentInsetDidChange;
- (void)scrollViewDidChangeAdjustedContentInset:(UIScrollView*)scrollView;

TableView

苹果在iOS 8引入Self-Sizing ,通过设置estimatedRowHeight属性可以实现TableView的 Cell 可变行高。

Self-Sizing在 iOS 11中默认开启,Headers, footers, and cells都默认开启Self-Sizing,所有estimatedRowHeight 默认值也从0变为iOS11的UITableViewAutomaticDimension

@property (nonatomic) CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0); // default is UITableViewAutomaticDimension, set to 0 to disable

如果iOS 11之前项目中没有使用estimateRowHeight属性,可以通过以下方式关闭:

self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

iOS 7在UITableView中 引入 separatorInset 属性,用以设置 cell 的分割线边距,在 iOS 11 中对其进行了扩展。可以通过新增的separatorInsetReference属性来设置separatorInset属性的值,该属性是UITableViewSeparatorInsetReference枚举类型的。

typedef NS_ENUM(NSInteger, UITableViewSeparatorInsetReference) {
UITableViewSeparatorInsetFromCellEdges, UITableViewSeparatorInsetFromAutomaticInsets
}

TableView滑动操作

苹果在 iOS 8中为UITableView增加原生右滑操作接口,新增了一个代理方法(tableView: editActionsForRowAtIndexPath:)和一个类(UITableViewRowAction),代理方法返回的是一个数组,在代理方法中定义需要的操作(删除、转发等),这些操作是UITableViewRowAction的对象。从iOS 11可以为这些操作按钮增加图片,如果实现了以下两个iOS 11新代理方法,将会取代(tableView: editActionsForRowAtIndexPath:)代理方法:

- (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 *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//.....
}];
//......
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];
return config;
}

创建UIContextualAction对象时,UIContextualActionStyle有两种类型,如果是置顶、转发等按钮就使用UIContextualActionStyleNormal类型,delete操作按钮使用UIContextualActionStyleDestructive类型,当是删除类型时,一直向右滑动cell,会直接执行删除操作。枚举类型如下:

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

其中还有一个小细节,如果cell高度相对较小时,title是不显示的,会只显示image,只有当cell高度比较高时,才同时显示image和title。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容