Skip to content

Commit 0cd94ba

Browse files
committed
Added background label to back root view to disable scale to the scroll view.
1 parent bd43301 commit 0cd94ba

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

.DS_Store

0 Bytes
Binary file not shown.

AXWebViewController/AXWebViewController/AXWebViewController.m

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ - (void)dealloc {
363363
_webView.UIDelegate = nil;
364364
_webView.navigationDelegate = nil;
365365
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
366+
[_webView removeObserver:self forKeyPath:@"scrollView.contentOffset"];
366367
/*
367368
[_webView.scrollView removeObserver:self forKeyPath:@"backgroundColor"];
368369
*/
@@ -407,6 +408,10 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
407408
}
408409
*/
409410
#endif
411+
} else if ([keyPath isEqualToString:@"scrollView.contentOffset"]) {
412+
// Get the current content offset.
413+
CGPoint contentOffset = [change[NSKeyValueChangeNewKey] CGPointValue];
414+
_backgroundLabel.transform = CGAffineTransformMakeTranslation(0, -contentOffset.y-_webView.scrollView.contentInset.top);
410415
} else {
411416
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
412417
}
@@ -437,6 +442,8 @@ - (WKWebView *)webView {
437442
_webView.translatesAutoresizingMaskIntoConstraints = NO;
438443
_webView.UIDelegate = self;
439444
_webView.navigationDelegate = self;
445+
// Obverse the content offset of the scroll view.
446+
[_webView addObserver:self forKeyPath:@"scrollView.contentOffset" options:NSKeyValueObservingOptionNew context:NULL];
440447
return _webView;
441448
}
442449

@@ -1388,9 +1395,6 @@ - (void)setupSubviews {
13881395

13891396
// Add web view.
13901397
#if AX_WEB_VIEW_CONTROLLER_USING_WEBKIT
1391-
[self.view addSubview:self.webView];
1392-
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_webView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_webView)]];
1393-
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topLayoutGuide][_webView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_webView, topLayoutGuide, bottomLayoutGuide)]];
13941398
// Set the content inset of scroll view to the max y position of navigation bar to adjust scroll view content inset.
13951399
// To fix issue: https://github.com/devedbox/AXWebViewController/issues/10
13961400
/*
@@ -1399,11 +1403,19 @@ - (void)setupSubviews {
13991403
_webView.scrollView.contentInset = contentInset;
14001404
*/
14011405

1402-
UIView *contentView = _webView.scrollView.subviews.firstObject;
1403-
[contentView addSubview:self.backgroundLabel];
1404-
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_backgroundLabel(<=width)]" options:0 metrics:@{@"width":@([UIScreen mainScreen].bounds.size.width)} views:NSDictionaryOfVariableBindings(_backgroundLabel)]];
1405-
[contentView addConstraint:[NSLayoutConstraint constraintWithItem:_backgroundLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
1406-
[contentView addConstraint:[NSLayoutConstraint constraintWithItem:_backgroundLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:-20]];
1406+
// Add background label to view.
1407+
// UIView *contentView = _webView.scrollView.subviews.firstObject;
1408+
[self.view addSubview:self.backgroundLabel];
1409+
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_backgroundLabel(<=width)]" options:0 metrics:@{@"width":@(self.view.bounds.size.width)} views:NSDictionaryOfVariableBindings(_backgroundLabel)]];
1410+
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:_backgroundLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
1411+
// [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_backgroundLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:-20]];
1412+
1413+
[self.view addSubview:self.webView];
1414+
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_webView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_webView)]];
1415+
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topLayoutGuide][_webView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_webView, topLayoutGuide, bottomLayoutGuide, _backgroundLabel)]];
1416+
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_backgroundLabel]-20-[_webView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_backgroundLabel, _webView)]];
1417+
1418+
[self.view bringSubviewToFront:_backgroundLabel];
14071419
#else
14081420
[self.view insertSubview:self.backgroundLabel atIndex:0];
14091421
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8-[_backgroundLabel]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_backgroundLabel)]];

0 commit comments

Comments
 (0)