WKWebView进度条

首先设置代理

1
self.webView.navigationDelegate=self;
1
<WKNavigationDelegate>

然后在开始加载的时候

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma mark 页面开始加载时调用
-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
self.progressLine.hidden=NO;

__weak typeof(self) weakSelf = self;

weakSelf.progressLineWidth.constant=0;

dispatch_async(dispatch_get_main_queue(), ^{

[UIView animateWithDuration:0.4 animations:^{

weakSelf.progressLineWidth.constant=WIDTH*0.5;

[weakSelf.view layoutIfNeeded];


} completion:^(BOOL finished) {

[UIView animateWithDuration:0.4 animations:^{

weakSelf.progressLineWidth.constant=WIDTH*0.8;

[weakSelf.view layoutIfNeeded];

}];
}];
});
}

加载完成时调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma mark 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {

__weak typeof(self) weakSelf = self;

[UIView animateWithDuration:0.2 animations:^{

weakSelf.progressLineWidth.constant=WIDTH;

[weakSelf.view layoutIfNeeded];

} completion:^(BOOL finished) {

weakSelf.progressLine.hidden=YES;

weakSelf.progressLineWidth.constant=0;

}];
}