背景:在iOS11系統(tǒng)上如果一個(gè)頁(yè)面里有UIScrollView
或者其子類的subView诬留,同時(shí)滿足該subView的top和view的top相等,
或者該subView的topLayoutGuide
和view的safeAreaLayoutGuide
的topAnchor
相等時(shí)盒刚,在頁(yè)面push和pop的過程中UIScrollView
會(huì)有一個(gè)詭異的往上跳動(dòng)的動(dòng)畫彩届。
問題出現(xiàn)的場(chǎng)景
- iOS11
- 頁(yè)面中至少有一個(gè)
UIScrollView
或者其子類的subView - subView滿足top和view的top相等或者subView的
topLayoutGuide
和view的safeAreaLayoutGuide
的topAnchor
相等
現(xiàn)象研究
經(jīng)過調(diào)試發(fā)現(xiàn)樟蠕,在頁(yè)面push和pop時(shí)scrollView的contentOffset被移動(dòng)了44。
進(jìn)一步調(diào)試發(fā)現(xiàn)push和pop頁(yè)面時(shí)UIViewController
的
- (void)viewSafeAreaInsetsDidChange;
方法都會(huì)被調(diào)用吓懈。
- (void)viewSafeAreaInsetsDidChange
{
[super viewSafeAreaInsetsDidChange];
CGRect layoutFrame = self.view.safeAreaLayoutGuide.layoutFrame;
NSLog(@"%@", NSStringFromCGRect(layoutFrame));
}
打印結(jié)果:
- pop:
{{0, 44}, {375, 646}}
{{0, 0}, {375, 690}}
- push:
{{0, 0}, {375, 690}}
{{0, 44}, {375, 646}}
{{0, 0}, {375, 690}}
這樣會(huì)導(dǎo)致在push和pop時(shí)scrollView的contentOffset會(huì)往上移動(dòng)44耻警,所以出現(xiàn)了上述現(xiàn)象甸怕。
至于移動(dòng)的44具體是什么值暫不清楚。
解決方案
1. 設(shè)置UIScrollView
的contentInsetAdjustmentBehavior
屬性為UIScrollViewContentInsetAdjustmentNever
温兼。
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
設(shè)置該屬性確實(shí)能解決push和pop時(shí)scrollView跳動(dòng)的問題武契,但是這樣在iPhone X上會(huì)導(dǎo)致新的問題出現(xiàn)咒唆。
設(shè)置之前由于該屬性的值是UIScrollViewContentInsetAdjustmentAutomatic
,這樣在iPhone X上系統(tǒng)能夠自動(dòng)處理好scrollView的contentInset装处,這樣能保證iPhone X底部的虛擬Home鍵不會(huì)擋住ScrollView內(nèi)容恨溜,在ScrollView滑動(dòng)到底部時(shí)會(huì)自動(dòng)留出safeArea的間距,確保內(nèi)容不會(huì)和虛擬Home鍵重疊判族。
當(dāng)設(shè)置成UIScrollViewContentInsetAdjustmentNever
后系統(tǒng)便不再幫我們處理ScrollView的contentInset了形帮,表現(xiàn)就是列表底部?jī)?nèi)容和滑動(dòng)指示條會(huì)被虛擬Home鍵擋住。
2. 將VC的edgesForExtendedLayout
屬性設(shè)置成UIRectEdgeNone
界斜。
在SO上有人提到過用這種方案來解決scrollview跳動(dòng)問題各薇,但是該方案在我們項(xiàng)目中測(cè)試并沒有作用君躺,所以沒有采用這種方法。
3. 在self.view中insert一個(gè)空白view林螃。
在調(diào)試時(shí)偶然發(fā)現(xiàn)當(dāng)UIView的subViews中的第一個(gè)subView不是UIScrollView或者其子類時(shí)在頁(yè)面push和pop時(shí)便不再出現(xiàn)ScrollView跳動(dòng)的問題俺泣,雖然為什么會(huì)這樣的原因暫時(shí)未知。
但這確實(shí)給我們解決問題提供了很大思路横漏,為此我們寫了一個(gè)Hook的工具熟掂,hook住UIViewController的viewDidLoad方法,并在self.view中任意insert一個(gè)大小為0的不可見的view,這樣便解決了這個(gè)問題尊蚁。
#ifdef __IPHONE_11_0
#import <Objc/runtime.h>
static const void *kFakeDotViewKey = &kFakeDotViewKey;
@implementation UIViewController (HLScrollViewJumpFix)
- (UIView *)fakeDotView
{
return objc_getAssociatedObject(self, kFakeDotViewKey);
}
- (void)setFakeDotView:(UIView *)fakeDotView
{
objc_setAssociatedObject(self, kFakeDotViewKey, fakeDotView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+ (void)p_swizzledMethodWithOriginCls:(Class)originCls originSel:(SEL)originSel
overridedCls:(Class)overridedCls overridedSel:(SEL)overridedSel
{
Method originalMethod = class_getInstanceMethod(originCls, originSel);
Method overrideMethod = class_getInstanceMethod(overridedCls, overridedSel);
if (originalMethod == NULL || overrideMethod == NULL) {
return;
}
BOOL success = class_addMethod(originCls, originSel, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod));
if (success) {
class_replaceMethod(overridedCls, overridedSel, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, overrideMethod);
}
}
+ (void)load
{
if (@available(iOS 11, *)) {
Class vcCls = self;
[self p_swizzledMethodWithOriginCls:vcCls originSel:@selector(viewDidLoad)
overridedCls:vcCls overridedSel:@selector(p_viewDidLoad)];
}
}
- (void)p_viewDidLoad
{
[self p_viewDidLoad];
Class cls = object_getClass(self);
const char *clsName = class_getName(cls);
NSString *clsNameString = [NSString stringWithUTF8String:clsName];
//只處理業(yè)務(wù)層的VC横朋,系統(tǒng)VC跳過
if (![clsNameString hasPrefix:@"HL"]) {
return;
}
UIView *fakeDotView = [self fakeDotView];
if (fakeDotView) {
return;
}
fakeDotView = [[UIView alloc] init];
[self.view insertSubview:fakeDotView atIndex:0];
[self setFakeDotView:fakeDotView];
}
@end
#endif
經(jīng)測(cè)試在VC的view中insert一個(gè)空白的view之后頁(yè)面在pop或push時(shí)UIScrollView
不再出現(xiàn)跳動(dòng)的問題琴锭。