博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS UI基础-21 WKWebView
阅读量:6090 次
发布时间:2019-06-20

本文共 3902 字,大约阅读时间需要 13 分钟。

WKWebView,直接显示网页,需要引入第三方类:

加上进度条代码,很完美使用

webView.h

#import 
@interface ECBankCardAddViewController : UIViewController/** 请求H5的时候,需要传的token */@property (nonatomic, copy) NSString *token;@end

 webView.m

////  ECBandCardAddViewController.m//  ECardBusiness////  Created by yongsheng.jiang on 2018/2/6.//  Copyright © 2018年 bige. All rights reserved.//#import "ECBankCardAddViewController.h"#import "WebViewJavascriptBridge.h"@interface ECBankCardAddViewController ()
@property (nonatomic, strong) WKWebView *webView;@property (nonatomic, weak) UIProgressView *progressView;@end@implementation ECBankCardAddViewController- (void)dealloc { [self.webView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))]; self.webView.navigationDelegate = nil;}- (void)viewDidLoad { [super viewDidLoad]; self.title = @"添加银行卡"; self.view.backgroundColor = [UIColor whiteColor]; WKWebView *webView = [[NSClassFromString(@"WKWebView") alloc] initWithFrame:self.view.bounds]; [self.view addSubview:webView]; self.webView = webView; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",web_add_bandcard,self.token]]]; [webView loadRequest:request]; [webView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil]; [self setupProgressView];}- (void)setupProgressView { UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; progressView.frame = CGRectMake(0, 64, SCREEN_WIDTH, 5); [progressView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]]; progressView.progressTintColor = [UIColor greenColor]; [self.view addSubview:progressView]; _progressView = progressView;}#pragma mark - WKNavigationDelegate- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation { NSLog(@"didStartProvisionalNavigation"); //开始加载的时候,让进度条显示 self.progressView.hidden = NO;}- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation { NSLog(@"didCommitNavigation");}- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { NSLog(@"didFinishNavigation");}#pragma mark - KVO//kvo 监听进度-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary
*)change context:(void *)context{ if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.webView) { [self.progressView setAlpha:1.0f]; BOOL animated = self.webView.estimatedProgress > self.progressView.progress; [self.progressView setProgress:self.webView.estimatedProgress animated:animated]; if (self.webView.estimatedProgress >= 1.0f) { [UIView animateWithDuration:0.3f delay:0.3f 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]; }}@end

参考文档:

 

转载地址:http://jgpwa.baihongyu.com/

你可能感兴趣的文章
数组扩展方法之求和
查看>>
astah-professional-7_2_0安装
查看>>
函数是对象-有属性有方法
查看>>
uva 10107 - What is the Median?
查看>>
Linux下基本栈溢出攻击【转】
查看>>
c# 连等算式都在做什么
查看>>
使用c:forEach 控制5个换行
查看>>
java web轻量级开发面试教程摘录,java web面试技巧汇总,如何准备Spring MVC方面的面试...
查看>>
使用ansible工具部署ceph
查看>>
linux系列博文---->深入理解linux启动运行原理(一)
查看>>
Android反编译(一) 之反编译JAVA源码
查看>>
结合当前公司发展情况,技术团队情况,设计一个适合的技术团队绩效考核机制...
查看>>
python-45: opener 的使用
查看>>
cad图纸转换完成的pdf格式模糊应该如何操作?
查看>>
Struts2与Struts1区别
查看>>
网站内容禁止复制解决办法
查看>>
Qt多线程
查看>>
我的友情链接
查看>>
想说一点东西。。。。
查看>>
css知多少(8)——float上篇
查看>>