您的当前位置:首页iOS UIWebView加载时添加进度条

iOS UIWebView加载时添加进度条

2024-12-13 来源:哗拓教育

标注:此框架仅适合UIWebView  对iOS8后新出的WKWebView不适用,当然,你可以尝试修改框架里的几个代理方法。


导入头文件

#import "NJKWebViewProgressView.h"

#import "NJKWebViewProgress.h"

@implementation XFHelpCenterViewController

{

NJKWebViewProgressView *_progressView;

NJKWebViewProgress *_progressProxy;

}

- (void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];

[self.navigationController.navigationBar addSubview:_progressView];

}

-(void)viewWillDisappear:(BOOL)animated{

[super viewWillDisappear:animated];

[_progressView removeFromSuperview];

}


初始化

-(void)initViews{

XFUserModel* userModel = [NSKeyedUnarchiver unarchiveObjectWithFile:[NSString userDataPathWithPath:@"userIfor.data"]];

LGFLog(@"%@",userModel.help);

NSURL *URL = [NSURL URLWithString:userModel.help];

_webView = [[UIWebView alloc] initWithFrame:self.view.bounds];

[_webView loadRequest:[NSURLRequest requestWithURL:URL]];

[self.view addSubview:_webView];

}

-(void)setProgress{

_progressProxy = [[NJKWebViewProgress alloc] init];

self.webView.delegate = _progressProxy;

_progressProxy.webViewProxyDelegate = self;

_progressProxy.progressDelegate = self;

CGFloat progressBarHeight = 1.f;

CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;

CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height - progressBarHeight, navigationBarBounds.size.width, progressBarHeight);

_progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];

_progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;

}


代理方法

#pragma mark - NJKWebViewProgressDelegate

-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress{

[_progressView setProgress:progress animated:YES];

}

显示全文