Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use WKWebView to imitate Wechat to load progress bar in iOS

Shulou Source: shulou.com Published: 2022-06-03 09:21:10 09月11日 Update

This article mainly introduces how to use WKWebView to imitate Wechat in iOS to load the progress bar, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article, and let the editor take you to know it.

The details are as follows

WKWebView adds the estimatedProgress attribute (double type), which we can use to set the UIProgressView

Demo stored on the github code repository

Add a UIProgressView attribute to the page

@ property (nonatomic, strong) WKWebView * mywebView;@property (nonatomic, strong) UIProgressView * progressView;// sets the loading progress bar

Lazy load UIProgressView

-(UIProgressView *) progressView {if (! _ progressView) {_ progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; _ progressView.frame = CGRectMake (0,64, 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];} return _ progressView;}

Kvo adds monitoring when initializing WKWebView (when I am loading lazily)

[_ mywebView addObserver:self forKeyPath:NSStringFromSelector (@ selector (estimatedProgress)) options:0 context:nil]

Hide the progress bar when the page starts to load

/ / start loading-(void) webView: (WKWebView *) webView didStartProvisionalNavigation: (WKNavigation *) navigation {/ / when you start loading, let the progress bar display self.progressView.hidden = NO;}

Kvo listens for progress

/ / kvo snooping progress-(void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object change: (NSDictionary *) change context: (void *) context {if ([keyPath isEqualToString:NSStringFromSelector (@ selector (estimatedProgress))] & & object = = self.mywebView) {[self.progressView setAlpha:1.0f]; BOOL animated = self.mywebView.estimatedProgress > self.progressView.progress; [self.progressView setProgress:self.mywebView.estimatedProgress animated:animated] If (self.mywebView.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];}}

Remove listeners in the dealloc method

-(void) dealloc {[self.mywebView removeObserver:self forKeyPath:NSStringFromSelector (@ selector (estimatedProgress))];}

Thank you for reading this article carefully. I hope the article "how to use WKWebView to imitate Wechat to load progress bar in iOS" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

Tags: Progress articles properties monitoring pages warehouses code values interests content at the same time methods time is in more friends knowledge types editing industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Microsoft Shulou Technology Apple Redmi