MJRefresh和继承自UIScrollView的视图的iOS 11适配
背景:前两天一个同行问我了一个问题:
![]()
问题
还有就是说如果你使用了MJRefresh
进行刷新,并且你隐藏了导航栏,就会出现下拉刷新错乱的问题。
这跟我这哥们问的问题是一种类型的,因为iOS 11
上废除了automaticallyAdjustsScrollViewInsets
这个方法,使用UIScrollView's contentInsetAdjustmentBehavior
来代替,解决办法就是一段代码:
OC:
if (@available(iOS 11.0, *)) {
self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
swift:
if #available(iOS 11.0, *) {
tableView.contentInsetAdjustmentBehavior = .never
} else {
self.automaticallyAdjustsScrollViewInsets = false
}
因为哥们是OC写的项目,大家用的时候注意写成Swift。
.iOS 11 下tableView的头视图和脚视图
在iOS11里面有时候在tableView的头部和尾部留白,因为苹果给滚动试图加进去了
self-sizeing
,开始计算逐步计算contentSize
,默认如果不去实现viewForHeaderInSection
就不会调用heightForHeaderInSection
,尾部试图一样。
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return nil
}
如果你不想实现viewForHeaderInSection
也不想留白,那么只需要你把self-sizeing
自动估高关闭即可
/// 自动关闭估算高度,不想估算那个,就设置那个即可
self.tableView.estimatedRowHeight = 0
self.tableView.estimatedSectionHeaderHeight = 0
self.tableView.estimatedSectionFooterHeight = 0