@@ -17,6 +17,7 @@ @interface AFWebViewController () <WKNavigationDelegate>
1717// Bar buttons
1818@property (nonatomic , strong ) UIBarButtonItem *backBarButtonItem, *forwardBarButtonItem, *refreshBarButtonItem, *stopBarButtonItem, *actionBarButtonItem;
1919@property (nonatomic , strong ) NSURLRequest *request;
20+ @property (nonatomic , strong ) UIProgressView *progressView;
2021@end
2122
2223@implementation AFWebViewController
@@ -73,6 +74,7 @@ - (void)loadRequest:(NSURLRequest *)request {
7374- (void )dealloc
7475{
7576 self.webView .navigationDelegate = nil ;
77+ [self .webView removeObserver: self forKeyPath: @" estimatedProgress" ];
7678}
7779
7880- (void )loadView {
@@ -83,6 +85,7 @@ - (void)loadView {
8385- (void )viewDidLoad {
8486 [super viewDidLoad ];
8587 [self updateToolbarItems ];
88+ [self appendProgressView ];
8689}
8790
8891- (void )viewWillAppear : (BOOL )animated {
@@ -111,6 +114,11 @@ - (void)viewDidDisappear:(BOOL)animated {
111114 [UIApplication sharedApplication ].networkActivityIndicatorVisible = NO ;
112115}
113116
117+ - (void )viewDidLayoutSubviews {
118+ [super viewDidLayoutSubviews ];
119+ self.progressView .frame = CGRectMake (0 , [self .topLayoutGuide length ], self.view .frame .size .width , 0.5 );
120+ }
121+
114122- (BOOL )shouldAutorotateToInterfaceOrientation : (UIInterfaceOrientation)toInterfaceOrientation {
115123 if (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiomPad)
116124 return YES ;
@@ -168,6 +176,13 @@ - (UIBarButtonItem *)actionBarButtonItem {
168176 return _actionBarButtonItem;
169177}
170178
179+ - (UIProgressView *)progressView {
180+ if (!_progressView) {
181+ _progressView = [[UIProgressView alloc ] initWithProgressViewStyle: UIProgressViewStyleDefault];
182+ }
183+ return _progressView;
184+ }
185+
171186#pragma mark - Toolbar
172187
173188- (void )updateToolbarItems {
@@ -260,6 +275,21 @@ - (void)actionButtonTapped:(id)sender {
260275 }
261276}
262277
278+ #pragma mark - ProgressView
279+
280+ - (void )appendProgressView {
281+ [self .view addSubview: self .progressView];
282+ [self .webView addObserver: self forKeyPath: @" estimatedProgress" options: NSKeyValueObservingOptionNew context: nil ];
283+ }
284+
285+ - (void )observeValueForKeyPath : (NSString *)keyPath ofObject : (id )object change : (NSDictionary *)change context : (void *)context {
286+ if ([keyPath isEqualToString: @" estimatedProgress" ]) {
287+ self.progressView .hidden = self.webView .estimatedProgress == 1 ;
288+ float progress = self.progressView .hidden ? 0 : self.webView .estimatedProgress ;
289+ [self .progressView setProgress: progress animated: true ];
290+ }
291+ }
292+
263293#pragma mark - WKNavigation delegate
264294
265295- (void )webView : (WKWebView *)webView didCommitNavigation : (WKNavigation *)navigation {
0 commit comments