WKWebView的进度条
WKWebView的回退上级网页
WKWebView的获取网页标题
//获取系统返回按钮的点击事件
-(BOOL)navigationShouldPopOnBackButton {
// [JJTools lightNavigation:self];
if (self.webView.canGoBack==YES) {
[self.webView goBack];
return NO;
}else{
return YES;
}
}
//进度条
_progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame),2)];
_progressView.tintColor = _kMainColor;
_progressView.trackTintColor= _kmainBackGroundColor;
[self.view addSubview:_progressView];
[_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld context:nil];
//监听标题网页标题
if (_shouldUpdataTitle) {
[_webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
}
//添加网页加载进度条获取标题
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
NSLog(@" %s,change = %@",__FUNCTION__,change);
if ([keyPath isEqual: @"estimatedProgress"] && object == _webView) {
[self.progressView setAlpha:1.0f];
[self.progressView setProgress:_webView.estimatedProgress animated:YES];
if(_webView.estimatedProgress >= 1.0f)
{
[UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.progressView setAlpha:0.0f];
} completion:^(BOOL finished) {
[self.progressView setProgress:0.0f animated:NO];
}];
}
} else if ([keyPath isEqualToString:@"title"])
{
if (object == self.webView) {
self.title = self.webView.title;
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)dealloc {
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
//监听标题网页标题
if (_shouldUpdataTitle) {
[_webView removeObserver:self forKeyPath:@"title"];
}
// if you have set either WKWebView delegate also set these to nil here
[_webView setNavigationDelegate:nil];
[_webView setUIDelegate:nil];
}
网友评论