WKWebView添加加載進度條


WKWebView添加加載進度條

1,先懶加載一個進度條。

#pragma mark - ***** 進度條 - (UIProgressView *)progressView { if (!_progressView) { UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)]; progressView.tintColor = [UIColor blueColor]; progressView.trackTintColor = [UIColor magentaColor]; [self.view addSubview:progressView]; self.progressView = progressView; } return _progressView; }

2,初始化WKWebView。

WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds]; wkWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; wkWebView.backgroundColor = [UIColor whiteColor]; wkWebView.navigationDelegate = self; /*! 適應屏幕 */ // wkWebView.scalesPageToFit = YES; /*! 解決iOS9.2以上黑邊問題 */ wkWebView.opaque = NO; /*! 關閉多點觸控 */ wkWebView.multipleTouchEnabled = YES; /*! 加載網頁中的電話號碼,單擊可以撥打 */ // wkWebView.dataDetectorTypes = YES; [self.view insertSubview:wkWebView belowSubview:_progressView]; [wkWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_urlString]]; [wkWebView loadRequest:request]; self.wkWebView = wkWebView;

3,在KVO中設置相關邏輯

#pragma mark 計算wkWebView進度條
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (object == self.wkWebView && [keyPath isEqualToString:@"estimatedProgress"])
    {
        CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
        if (newprogress == 1)
        {
            self.progressView.hidden = YES;
            [self.progressView setProgress:0 animated:NO];
        }
        else
        {
            self.progressView.hidden = NO;
            [self.progressView setProgress:newprogress animated:YES];
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM