1、在webview頂部添加一個(gè)進(jìn)度條UIProgressView摧冀。
2菜拓、給webVIew添加一個(gè)監(jiān)聽屬性“estimatedProgress”瓣窄。
3、在監(jiān)聽事件中纳鼎,設(shè)置ProgressView 的進(jìn)度等于webview的estimatedProgress俺夕。
添加WKWebView,并給WKWebView添加監(jiān)聽事件
- (void)addWKWebView{
// 創(chuàng)建WKWebView
if (@available(iOS 8.0, *)) {
self.wkWebView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
} else {
// Fallback on earlier versions
}
_wkWebView.backgroundColor = [UIColor whiteColor];
_wkWebView.navigationDelegate = self;
// 設(shè)置訪問的URL
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
// 根據(jù)URL創(chuàng)建請(qǐng)求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// WKWebView加載請(qǐng)求
[_wkWebView loadRequest:request];
// 將WKWebView添加到視圖
[self.view addSubview:_wkWebView];
//給WKWebView添加監(jiān)聽
[_wkWebView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil];
}
給WKWebView添加ProgressView
- (void)addProgressView{
self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
_progressView.frame = CGRectMake(0, 44, SCREEN_WIDTH, 5);
[_progressView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
_progressView.progressTintColor = [UIColor greenColor];
[self.view addSubview:_progressView];
}
WKNavigationDelegate 代理方法
#pragma mark - WKNavigationDelegate
/**
開始加載
@param webView webView description
@param navigation navigation description
*/
-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation API_AVAILABLE(ios(8.0)){
NSLog(@"頁面開始加載時(shí)調(diào)用");
//開始加載的時(shí)候贱鄙,讓進(jìn)度條顯示
self.progressView.hidden = NO;
}
-(void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation API_AVAILABLE(ios(8.0)){
NSLog(@"didCommitNavigation");
}
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation API_AVAILABLE(ios(8.0)){
NSLog(@"didFinishNavigation");
}
監(jiān)聽方法實(shí)現(xiàn)
#pragma mark - KVO
//kvo 監(jiān)聽進(jìn)度
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))]
&& object == self.wkWebView) {
[self.progressView setAlpha:1.0f];
BOOL animated = self.wkWebView.estimatedProgress > self.progressView.progress;
[self.progressView setProgress:self.wkWebView.estimatedProgress
animated:animated];
if (self.wkWebView.estimatedProgress >= 1.0f) {
[UIView animateWithDuration:0.3f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.progressView setAlpha:0.0f];
} completion:^(BOOL finished) {
[self.progressView setProgress:0.0f animated:NO];
}];
}
}else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
移除監(jiān)聽事件
-(void)dealloc {
[self.wkWebView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
self.wkWebView.navigationDelegate = nil;
}