iOS UIWebView 之 UIProgressView

之前做等待跳转都是用UIActivityIndicatorView ,后来做webView 加载页面的时候,发现了一个特别好用又超级炫酷的加载提示NJKWebViewProgress,作者巧妙的通过计算加载请求的个数,用百分比展示进度,并且集成了加载页面的title,效果惊艳业界。今将使用步骤记录,方便日后查询,也希望能给朋友们带来一点帮助。

1、首先将炫酷的NJKWebViewProgress 拖到工程,赶紧"占为已有"

2、在加载页面铺以下代码,由于对NJKWebViewProgress的理解都是个人的,就不一一注释了,以免不到之处误导查询者。

 

@interface ViewController ()<UIWebViewDelegate,NJKWebViewProgressDelegate>{

    UIWebView *_webView;
    NJKWebViewProgressView *_progressView;
    NJKWebViewProgress *_progressProxy;
    NSURLRequest *req;
    
    BOOL cameraBool;
    UIImagePickerController *cameraPick;
    
    
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,WIDTH, HEIGHT)];
    _webView.scalesPageToFit = YES;
    [self.view addSubview:_webView];
    
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(replyEvent)];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshEvent)];
    
    self.navigationItem.leftBarButtonItem = item;
    self.navigationItem.rightBarButtonItem = item1;

    _progressProxy = [[NJKWebViewProgress alloc] init];
    _webView.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    
    CGFloat progressBarHeight = 2.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;

    [self jump];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar addSubview:_progressView];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_progressView removeFromSuperview];
}


-(void)replyEvent{
    
    [_webView goBack];
}

-(void)refreshEvent{
    [_webView reload];
}

-(void)jump{
    
    if(self.k == 1){
        req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.baidu.com/index.php?tn=monline_3_dg"]];}
//    }else if (self.k == 2){
//    req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.cnblogs.com/wangyang1213/"]];
//    }else if(self.k == 3){
//       req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.163.com/"]];
//    }
    else{
    req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://cc.yztcai.com/h5.php"]];
    }
    
    [_webView loadRequest:req];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    if(error != NULL){
        UIAlertController *alertController1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络连接异常" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction1 = [UIAlertAction actionWithTitle:@"确定" style:0 handler:nil];
        UIAlertAction *cancelAction1 = [UIAlertAction actionWithTitle:@"取消" style:0 handler:nil];
        [alertController1 addAction:sureAction1];
        [alertController1 addAction:cancelAction1];
        [self presentViewController:alertController1 animated:YES completion:nil];
    }
}

-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_progressView setProgress:progress animated:YES];
    self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

 

至此便集成了NJKWebViewProgress。拖到工程试试。

转载于:https://www.cnblogs.com/wangyang1213/p/5286101.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值