@@ -363,6 +363,7 @@ - (void)dealloc {
363
363
_webView.UIDelegate = nil ;
364
364
_webView.navigationDelegate = nil ;
365
365
[_webView removeObserver: self forKeyPath: @" estimatedProgress" ];
366
+ [_webView removeObserver: self forKeyPath: @" scrollView.contentOffset" ];
366
367
/*
367
368
[_webView.scrollView removeObserver:self forKeyPath:@"backgroundColor"];
368
369
*/
@@ -407,6 +408,10 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
407
408
}
408
409
*/
409
410
#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 );
410
415
} else {
411
416
[super observeValueForKeyPath: keyPath ofObject: object change: change context: context];
412
417
}
@@ -437,6 +442,8 @@ - (WKWebView *)webView {
437
442
_webView.translatesAutoresizingMaskIntoConstraints = NO ;
438
443
_webView.UIDelegate = self;
439
444
_webView.navigationDelegate = self;
445
+ // Obverse the content offset of the scroll view.
446
+ [_webView addObserver: self forKeyPath: @" scrollView.contentOffset" options: NSKeyValueObservingOptionNew context: NULL ];
440
447
return _webView;
441
448
}
442
449
@@ -1388,9 +1395,6 @@ - (void)setupSubviews {
1388
1395
1389
1396
// Add web view.
1390
1397
#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)]];
1394
1398
// Set the content inset of scroll view to the max y position of navigation bar to adjust scroll view content inset.
1395
1399
// To fix issue: https://github.com/devedbox/AXWebViewController/issues/10
1396
1400
/*
@@ -1399,11 +1403,19 @@ - (void)setupSubviews {
1399
1403
_webView.scrollView.contentInset = contentInset;
1400
1404
*/
1401
1405
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];
1407
1419
#else
1408
1420
[self .view insertSubview: self .backgroundLabel atIndex: 0 ];
1409
1421
[self .view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @" H:|-8-[_backgroundLabel]-8-|" options: 0 metrics: nil views: NSDictionaryOfVariableBindings(_backgroundLabel)]];
0 commit comments