iOS UIWebView載入時新增進度條01

碼鋒窩發表於2016-12-23

標註:此框架僅適合UIWebView  對iOS8後新出的WKWebView不適用,當然,你可以嘗試修改框架裡的幾個代理方法。

框架是:NJKWebViewProgress

匯入標頭檔案

#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];
}


 

相關文章