#import "ViewController.h"
#include <WebKit/WebKit.h>
@interface ViewController ()
@property(nonatomic,strong)WKWebView *testWebView;
@property(nonatomic,strong)UIProgressView *progressView;
@end
// 屏幕高度
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
// 屏幕宽度
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化 wkwebView
_testWebView = [[WKWebView alloc]init];
_testWebView.frame = CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT);
[self.view addSubview:_testWebView];
// 设置进度条
_progressView = [[UIProgressView alloc]init];
_progressView.frame = CGRectMake(0, 20, SCREEN_WIDTH, 5);
_progressView.backgroundColor = [UIColor greenColor];
[self.view addSubview:_progressView];
// 添加监测
[_testWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld context:nil];
[_testWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
// Do any additional setup after loading the view, typically from a nib.
}
// 实现监测
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
NSLog(@" %s,change = %@",__FUNCTION__,change);
if ([keyPath isEqual: @"estimatedProgress"] && object == _testWebView) {
[self.progressView setAlpha:1.0f];
[self.progressView setProgress:_testWebView.estimatedProgress animated:YES];
if(_testWebView.estimatedProgress >= 1.0f)
{
[UIView animateWithDuration:0.3 delay:0.3 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];
}
}
- (void)dealloc {
[_testWebView removeObserver:self forKeyPath:@"estimatedProgress"];
// if you have set either WKWebView delegate also set these to nil here
[_testWebView setNavigationDelegate:nil];
[_testWebView setUIDelegate:nil];
}
WKWebView添加进度条 - iOS
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一、效果展示 二、主要步骤 1.添加UIProgressView属性 2.初始化progressView 3.添加...