From 8ce832e18a310b12b0ad687f79b054cb441f94e2 Mon Sep 17 00:00:00 2001 From: devedbox <862099730@qq.com> Date: Sun, 1 Jan 2017 21:14:23 +0800 Subject: [PATCH] Fixed the issue which the scroll view's content inset changed when trigger the back action on iOS 10.2. --- .../AXWebViewController/AXWebViewController.m | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/AXWebViewController/AXWebViewController/AXWebViewController.m b/AXWebViewController/AXWebViewController/AXWebViewController.m index 6ab7e4f..3690b63 100644 --- a/AXWebViewController/AXWebViewController/AXWebViewController.m +++ b/AXWebViewController/AXWebViewController/AXWebViewController.m @@ -144,8 +144,8 @@ - (instancetype)initWithHTMLString:(NSString *)HTMLString baseURL:(NSURL *)baseU - (void)viewDidLoad { [super viewDidLoad]; - - self.automaticallyAdjustsScrollViewInsets = YES; + // Change auto just scroll view insets to NO to fix issue: https://github.com/devedbox/AXWebViewController/issues/10 + self.automaticallyAdjustsScrollViewInsets = NO; self.extendedLayoutIncludesOpaqueBars = YES; /* Using contraints to view instead of bottom layout guide. self.edgesForExtendedLayout = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeRight; @@ -319,6 +319,19 @@ - (void)dealloc { #endif } +#pragma mark - Override. +- (void)setAutomaticallyAdjustsScrollViewInsets:(BOOL)automaticallyAdjustsScrollViewInsets { + // Auto adjust scroll view content insets will always be false. + [super setAutomaticallyAdjustsScrollViewInsets:NO]; + /* + // Remove web view from super view and then set up from beginning. + [self.view removeConstraints:self.view.constraints]; + [_webView removeFromSuperview]; + // Do set up web views. + [self setupSubviews]; + */ +} + #pragma mark - KVO - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"estimatedProgress"]) { @@ -1215,7 +1228,15 @@ - (void)setupSubviews { #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0 [self.view addSubview:self.webView]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_webView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_webView)]]; - [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_webView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_webView, topLayoutGuide, bottomLayoutGuide)]]; + [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topLayoutGuide][_webView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_webView, topLayoutGuide, bottomLayoutGuide)]]; + // Set the content inset of scroll view to the max y position of navigation bar to adjust scroll view content inset. + // To fix issue: https://github.com/devedbox/AXWebViewController/issues/10 + /* + UIEdgeInsets contentInset = _webView.scrollView.contentInset; + contentInset.top = CGRectGetMaxY(self.navigationController.navigationBar.frame); + _webView.scrollView.contentInset = contentInset; + */ + UIView *contentView = _webView.scrollView.subviews.firstObject; [contentView addSubview:self.backgroundLabel]; [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_backgroundLabel(<=width)]" options:0 metrics:@{@"width":@([UIScreen mainScreen].bounds.size.width)} views:NSDictionaryOfVariableBindings(_backgroundLabel)]];