美文网首页
iOS开发-WebView添加进度条

iOS开发-WebView添加进度条

作者: 我是卖报的小行家 | 来源:发表于2020-12-31 16:01 被阅读0次

当我们在使用WKWebView加载HTML页面时候,能够看到进度条往往会是一个不错的体验,今天就来个初级的webview添加进度条
1.首先添加UIProgressView;

@property(nonatomic,strong)UIProgressView * progressView;

2.初始化progressView

self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 40,   self.view.frame.size.width, 3)];
self.progressView.progressViewStyle = UIProgressViewStyleBar;
self.progressView.tintColor = [UIColor systemBlueColor];
[self.view addSubview:self.progressView];
self.progressView.hidden = true;

3.webview使用KVO检测进度条当前进度

[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];

4.kvo监测

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    self.progressView.progress = self.webView.estimatedProgress;
}

5.在一些环境中设置progressView的显示与否

//加载完成隐藏
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
   self.progressView.hidden = true;
}
//页面消失时候隐藏
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [_progressView removeFromSuperview];
    _progressView.hidden = true;
}
//开始加载页面时候显示
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    _progressView.hidden = false;
}
//加载页面失败时候隐藏
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    _progressView.hidden = true;
}

相关文章

网友评论

      本文标题:iOS开发-WebView添加进度条

      本文链接:https://www.haomeiwen.com/subject/dwfroktx.html