前两天Xcode版本更新到了GM Seed版本。正好有需要适配iOS11,就下载了下来试试看。
然后发现用到了下拉刷新的地方奇丑无比,下拉刷新漏了一半在外面。
如图:
透视发现这个TableView的contentInset为(20,0,0,0),经过一番查找,在UIScrollView的.h文件中发现了一个新增的属性:
/* Configure the behavior of adjustedContentInset.
Default is UIScrollViewContentInsetAdjustmentAutomatic.
*/
@property(nonatomic) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior API_AVAILABLE(ios(11.0),tvos(11.0));
这个属性的默认值是UIScrollViewContentInsetAdjustmentAutomatic,
再看看这个iOS11新增的枚举:
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的注释说明:
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
大概意思是说当一个ScrollView是一个控制器的View的子View并且navigation controller中automaticallyAdjustsScrollViewInsets = YES的时候,会自动调整contentInset的top和Bottom。
根据这些资料首先尝试了修改UIScrollView的UIScrollViewContentInsetAdjustmentBehavior这个属性的默认值,因为项目中很多地方都用到了UITableView以及UIScrollView,一个一个改太麻烦了,所以想到了runtime,直接上代码吧:
#import "UIScrollView+contentInset.h"
@implementation UIScrollView (contentInset)
+ (void)load {
[super load];
//因为是为了适配iOS11 所以只有在系统是iOS11的时候用过运行时修改这个值
if (iOS11) {
Method originalM = class_getInstanceMethod([self class], @selector(initWithFrame:));
Method exchangeM = class_getInstanceMethod([self class], @selector(cl_initWithFrame:));
method_exchangeImplementations(originalM, exchangeM);
}
}
- (instancetype)cl_initWithFrame:(CGRect)frame {
if (@available(iOS 11.0, *)) {
self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
}
return [self cl_initWithFrame:frame];
}
@end
ps:附上修改之后的图片: