ios 11 上tableview 改动
1、 UIViewController的automaticallyAdjustsScrollViewInsets
属性被废弃。需要使用UIScrollView的contentInsetAdjustmentBehavior
来替代。
关于 contentInsetAdjustmentBehavior
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
UIScrollViewContentInsetAdjustmentAutomatic, // Similar to .scrollableAxes, but for backward compatibility will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewInsets = YES inside a navigation controller, regardless of whether the scroll view is scrollable
UIScrollViewContentInsetAdjustmentScrollableAxes, // Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)
UIScrollViewContentInsetAdjustmentNever, // contentInset is not adjusted
UIScrollViewContentInsetAdjustmentAlways, // contentInset is always adjusted by the scroll view's safeAreaInsets
} API_AVAILABLE(ios(11.0),tvos(11.0));
- UIScrollViewContentInsetAdjustmentAutomatic 滚动视图会自动计算和适应顶部和底部的内边距并且在滚动视图不可滚动时,也会设置内边距
- UIScrollViewContentInsetAdjustmentScrollableAxes 自动计算内边距。
*UIScrollViewContentInsetAdjustmentNever 不计算内边距
*UIScrollViewContentInsetAdjustmentAlways 根据safeAreaInsets计算内边距
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
2、iOS 11中如果不实现-tableView: viewForFooterInSection:
和 -tableView: viewForHeaderInSection:
,那么-tableView: heightForHeaderInSection:
和- tableView: heightForFooterInSection:
不会被调用。
这是因为estimatedRowHeight
estimatedSectionHeaderHeight
estimatedSectionFooterHeight
三个高度估算属性由默认的0
变成了UITableViewAutomaticDimension
,导致高度计算不对,解决方法是实现对应方法或吧这三个属性设为0。
下面这个列表显示不全也是estimatedRowHeight引起,取contentSize出错。
3、在philm项目中我需要把默认选中的滤镜自动移动到屏幕的中间时
直接使用:
//这样写 并不work
[self.filterListView scrollToRowAtIndexPath:self.selectedIndexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:NO];
// 加上0.1秒的延迟就好了 https://stackoverflow.com/questions/46075517/uitableview-scrolltorow-no-longer-works-on-ios-11-right-after-adding-a-new-row
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.filterListView scrollToRowAtIndexPath:self.selectedIndexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:NO];
});