123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- //
- // KDPWebVC.m
- // KuDianProject
- //
- // Created by 学丽 on 2019/7/9.
- // Copyright © 2019 KDP. All rights reserved.
- //
- #import "KDPWebVC.h"
- @interface KDPWebVC ()<WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler>
- @property(nonatomic,strong)WKWebView *webViews;
- @property(nonatomic,strong)UIButton *returnBtn;
- @property(nonatomic,strong)UIButton *closeBtn;
- @property(nonatomic,strong)UIButton *refreshBtn;
- @property(nonatomic,strong)UIProgressView *progressBarView;
- @end
- @implementation KDPWebVC
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor=[UIColor colorWithHexString:@"#F4F4F4"];
-
- [self.navBar addleftReturnButton:self selector:@selector(returnClickBtn)];
-
- self.webViews =[[WKWebView alloc]initWithFrame:CGRectMake(0, KDNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-KDNavBarHeight)];
- self.webViews.backgroundColor=[UIColor colorWithHexString:@"F4F4F4"];
- self.webViews.navigationDelegate=self;
- self.webViews.UIDelegate=self;
- [ self.webViews addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil];
- //手势触摸滑动
- self.webViews.allowsBackForwardNavigationGestures = YES;
- //自适应
- [ self.webViews sizeToFit];
- //自适应
- [ self.webViews sizeToFit];
- [self.view addSubview: self.webViews];
- self.progressBarView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
- self.progressBarView.frame = CGRectMake(0, KDNavBarHeight, self.view.frame.size.width, 2);
- //设置进度条的色彩
- [self.progressBarView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
- self.progressBarView.progressTintColor = [UIColor colorWithHexString:ThemeColor];
- [self.view addSubview:self.progressBarView];
- if (@available(iOS 11.0,*)) {
- self.webViews.scrollView.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;
- }else{
- self.automaticallyAdjustsScrollViewInsets = NO;
- }
-
- [self loadUrl];
- }
- -(void)loadUrl
- {
- if (self.webUrl.length>0) {
- NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:self.webUrl]];
- [self.webViews loadRequest:request];
- }
- }
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
- if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.webViews ) {
- [self.progressBarView setAlpha:1.0];
-
- [self.progressBarView setProgress:self.webViews.estimatedProgress ];
- if (self.webViews.estimatedProgress >= 1.0) {
- [UIView animateWithDuration:0.7 animations:^{
- [self.progressBarView setProgress:1.0 animated:YES];
- [self.progressBarView setAlpha:0.0];
-
- }];
-
- }
- }else{
- [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
- }
- }
- /**
- 当网页加载完成的时候调用
-
- @param webView web描述
- @param navigation 导航的描述
- */
- - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
- self.navigationItem.title=webView.title;
- self.navBar.navTitleLabel.text=webView.title;
- [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
-
- }
- - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
- //开始加载的时候,让进度条显示
- self.progressBarView.hidden = NO;
- self.progressBarView.progress = 0;
- self.progressBarView.alpha = 1.0;
- [UIView animateWithDuration:0.8 animations:^{
- self.progressBarView.progress = 0.6;
-
- }];
- }
- //观察的移除
- - (void)dealloc{
- [self.webViews removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
- }
- -(void)returnClickBtn
- {
- [self.navigationController popViewControllerAnimated:YES];
- }
- -(void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- self.navigationController.navigationBar.hidden=YES;
- self.tabBarController.tabBar.hidden=YES;
- }
- -(void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- self.tabBarController.tabBar.hidden=NO;
- }
- @end
|