WKWebView简单使用-(KVO)加载进度条,标题,返回

ぐ巨炮叔叔 提交于 2019-12-04 06:44:10
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{//如果将cangoback写在此方法里面只能执行一次,如果当前网页跳转不是新的web就不能执行
}

//通过KVO的方式获取进度条,title,是否可以返回
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"title"]) {
        self.navigationItem.title = self.isShowTitle ? self.webView.title : @"";
    }else if ([keyPath isEqualToString:@"estimatedProgress"]){
        if (object == self.webView) {
            [_progressView setProgress:self.webView.estimatedProgress animated:true];
            if (self.webView.estimatedProgress >= 1.0) {
                [_progressView setProgress:0 animated:false];
            }
        }
    }else if ([keyPath isEqualToString:@"canGoBack"]){
        if (self.webView.canGoBack) {
            UIBarButtonItem * leftBtn1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhige_chat_back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];

            UIBarButtonItem * leftBtn2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"colseWeb"] style:UIBarButtonItemStylePlain target:self action:@selector(popView)];
            leftBtn2.imageInsets =  UIEdgeInsetsMake(0, -30, 0, 0);
            self.navigationItem.leftBarButtonItems = @[leftBtn1,leftBtn2];
        }else{
            UIBarButtonItem * leftBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhige_chat_back"] style:UIBarButtonItemStylePlain target:self action:@selector(popView)];
            self.navigationItem.leftBarButtonItems = @[leftBtn];

        }
        
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!