webView 添加网页来源

UIWebView:

//初始化webView时候添加一个label
  UIScrollView *scrollView = (UIScrollView *)webview.subviews[0];
  scrollView.bounces = YES;
  UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, scrollView.frame.size.width, 20)];
  label.tag = 999;
  label.numberOfLines = 0;
  label.textAlignment = NSTextAlignmentCenter;
  label.textColor = [UIColor darkGrayColor];
  label.text = @"网页由 www.baidu.com 提供";
  label.font = [UIFont systemFontOfSize:15];
  [webview insertSubview:label belowSubview:scrollView];


//在webView即将加载URL的代理方法里改变label的值
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
  UILabel * label = [webview viewWithTag:999];
  NSURL* url = request.URL;
  label.text = [NSString stringWithFormat:@"网页由 %@ 提供", url.host];
  return YES;
}

WKWebView:

//初始化webView时候添加一个label
UIScrollView* scrollView = _wkWebView.scrollView;
scrollView.bounces = YES;
scrollView.delegate = self;
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, 20)];
label.tag = 999;
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor darkGrayColor];
label.text = @"网页由 www.baidu.com 提供";
label.font = [UIFont systemFontOfSize:15];
label.hidden = YES;
   
//在代理里边改变label的值
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    UILabel* label = [_wkWebView viewWithTag:999];
    label.text = [NSString stringWithFormat:@"网页由 %@ 提供", webView.URL.host];
}

//需要控制label的显示隐藏
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    UILabel* label = [_wkWebView viewWithTag:999];
    //这个判断偏移的大小 是label在webView上的位置决定的
    if (scrollView.contentOffset.y<-30) {
        label.hidden = NO;
    }else {
        label.hidden = YES;
    }
}

你可能感兴趣的:(webView 添加网页来源)