緣由:今天在微信群里發(fā)現(xiàn)有朋友在聊到這個(gè)話題,一般得到的答案是 NJKWebViewProgress费尽,這個(gè)確實(shí)不錯(cuò),往往我們直接用它就OK啦泼各。但是我記得 iOS 8.0 之后夷磕,WKWebView 中已經(jīng)增加了一個(gè)
estimatedProgress
的屬性履肃。
estimatedProgress 屬性
就是利用KVO,觀察一下其加載情況, 自己加一個(gè)進(jìn)度條配合estimatedProgress
就好啦
[self.webView addObserver:self
forKeyPath:@"estimatedProgress"
options:NSKeyValueObservingOptionNew
context:nil];
[self.webView removeObserver:self forKeyPath:@"estimatedProgress" context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSString *,id> *)change
context:(void *)context {
if ([keyPath isEqualToString:@"estimatedProgress"]) {}
}
progress.gif
一個(gè)簡單的完整測試代碼坐桩,還是OK的
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) UIProgressView *progressView;
@end
@implementation ViewController
#pragma mark Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.webView];
[self.view addSubview:self.progressView];
[self.webView addObserver:self
forKeyPath:@"estimatedProgress"
options:NSKeyValueObservingOptionNew
context:nil];
}
- (void)dealloc {
[self.webView removeObserver:self forKeyPath:@"estimatedProgress" context:nil];
}
#pragma mark KVO
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSString *,id> *)change
context:(void *)context
{
if ([keyPath isEqualToString:@"estimatedProgress"]) {
self.progressView.progress = self.webView.estimatedProgress;
// 加載完成
if (self.webView.estimatedProgress >= 1.0f ) {
[UIView animateWithDuration:0.5f animations:^{
self.progressView.alpha = 0.0f;
self.progressView.progress = 0.0f;
}];
}else{
self.progressView.alpha = 1.0f;
}
}
}
#pragma mark Action
- (IBAction)loadingAction:(id)sender {
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
}
- (IBAction)refreshAction:(id)sender {
[self.webView reload];
}
#pragma mark LazyLoad
- (WKWebView *)webView {
if (!_webView) {
_webView = [[WKWebView alloc] init];
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
_webView.backgroundColor = [UIColor whiteColor];
_webView.scrollView.showsVerticalScrollIndicator = NO;
_webView.scrollView.showsHorizontalScrollIndicator = NO;
_webView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64);
}
return _webView;
}
- (UIProgressView *)progressView {
if (!_progressView) {
_progressView = [[UIProgressView alloc] init];
_progressView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 2);
}
return _progressView;
}
@end
當(dāng)然這是在iOS 8.0之后尺棋,我們利用
WKWebView
的屬性做的,iOS 8.0 之前還是用 NJKWebViewProgress 吧绵跷。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
如果還是不滿意膘螟,那就自定義,對UIWebViewDelegate 中的這幾個(gè)方法進(jìn)行自己的處理碾局,來顯示我們需要的效果荆残。