前兩天Xcode版本更新到了GM Seed版本。正好有需要適配iOS11,就下載了下來試試看。
然后發(fā)現(xiàn)用到了下拉刷新的地方奇丑無比,下拉刷新漏了一半在外面甚带。
如圖:
wrongRefresh.png
透視發(fā)現(xiàn)這個(gè)TableView的contentInset為(20,0,0,0),經(jīng)過一番查找,在UIScrollView的.h文件中發(fā)現(xiàn)了一個(gè)新增的屬性:
/* Configure the behavior of adjustedContentInset.
Default is UIScrollViewContentInsetAdjustmentAutomatic.
*/
@property(nonatomic) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior API_AVAILABLE(ios(11.0),tvos(11.0));
這個(gè)屬性的默認(rèn)值是UIScrollViewContentInsetAdjustmentAutomatic佳头,
再看看這個(gè)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
大概意思是說當(dāng)一個(gè)ScrollView是一個(gè)控制器的View的子View并且navigation controller中automaticallyAdjustsScrollViewInsets = YES的時(shí)候鹰贵,會(huì)自動(dòng)調(diào)整contentInset的top和Bottom。
根據(jù)這些資料首先嘗試了修改UIScrollView的UIScrollViewContentInsetAdjustmentBehavior這個(gè)屬性的默認(rèn)值康嘉,因?yàn)轫?xiàng)目中很多地方都用到了UITableView以及UIScrollView,一個(gè)一個(gè)改太麻煩了碉输,所以想到了runtime,直接上代碼吧:
#import "UIScrollView+contentInset.h"
@implementation UIScrollView (contentInset)
+ (void)load {
[super load];
//因?yàn)槭菫榱诉m配iOS11 所以只有在系統(tǒng)是iOS11的時(shí)候用過運(yùn)行時(shí)修改這個(gè)值
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:附上修改之后的圖片:
correct.png