美文网首页
WKWebView  进度条设置

WKWebView  进度条设置

作者: Luyc_Han | 来源:发表于2018-05-09 09:52 被阅读43次
    #import "wangmumu.h"
    #import <WebKit/WebKit.h>
    
    @interface wangmumu ()
    @property (nonatomic, strong) WKWebView *htmlWebView;
    @property (nonatomic, strong) UIProgressView *progressView;
    @end
    
    @implementation wangmumu
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        if ([self.isPop isEqualToString:@"pop"]) {
            [self setNavigationBarWhiteColor];
        } else {
            [self creatBack];
        }
    }
    
    - (void)createUI {
        [self.view addSubviewS:self.htmlWebView,self.progressView,nil];
        [self.htmlWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    }
    
    - (void)updateView {
        kWeakSelf(self)
        [self.htmlWebView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(weakself.view);
        }];
    }
    
    - (void)creatBack {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"Backicon"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"Backicon"] forState:UIControlStateHighlighted];
        [button setImage:[UIImage imageNamed:@"Backicon"] forState:UIControlStateSelected];
        button.imageEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
        button.frame = (CGRect){{0,0},{40,40}};
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    }
    
    #pragma mark - KVC and KVO
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
        if ([@"estimatedProgress" isEqualToString:keyPath]) {
            [self.progressView setProgress:self.htmlWebView.estimatedProgress animated:YES];
        }
        
        if (self.progressView.progress == 0) {
            self.progressView.hidden = NO;
        } else if (self.progressView.progress == 1) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (self.progressView.progress ==1) {
                    self.progressView.progress = 0;
                    self.progressView.hidden = YES;
                }
            });
        }
    }
    
    #pragma mark --- 实例化
    - (WKWebView *)htmlWebView {
        if (_htmlWebView == nil) {
            _htmlWebView = [[WKWebView alloc] init];
            NSURLRequest *requestUrl = [NSURLRequest requestWithURL:[NSURL URLWithString:self.getUrl]];
            [_htmlWebView loadRequest:requestUrl];
        }
        return _htmlWebView;
    }
    
    -(UIProgressView *)progressView {
        if (_progressView == nil) {
            CGRect  rect = CGRectZero;
            rect.size.width = [[UIScreen mainScreen]bounds].size.width;
            rect.size.height = 2;
            _progressView = [[UIProgressView alloc]initWithFrame:rect];
            _progressView.progressTintColor = [UIColor hexColor:@"#FFC400"];
            [_progressView setProgressViewStyle:UIProgressViewStyleDefault];
            [self.view addSubview:_progressView];
        }
        return _progressView;
    }
    
    - (void)dealloc {
        [self.htmlWebView removeObserver:self forKeyPath:@"estimatedProgress"];
        self.htmlWebView = nil;
        [self.htmlWebView removeFromSuperview];
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:WKWebView  进度条设置

          本文链接:https://www.haomeiwen.com/subject/jhqnrftx.html