iOS开发(OC)iOS开发杂货铺ios 知识点

WKWebView 添加头视图 及 WKWebView禁止放大缩

2018-12-04  本文已影响120人  tt大眼仔

WKWebView 添加头视图

代码如下:

// 初始化WKWebView
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.acs_width, self.view.acs_height - 48)];
    _webView.navigationDelegate = self;
    _webView.alpha = 0;
    [self.view insertSubview:_webView atIndex:0];

self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -429, self.view.frame.size.width, 429)];
// 设置偏移
CGFloat height = _headerView.frame.size.height;
_webView.scrollView.contentInset= UIEdgeInsetsMake(height,0,0,0);
[_webView.scrollView addSubview:self.headerView];
// webView加载数据
//_webView loadRequest:<#(nonnull NSURLRequest *)#>

最后还有一个代理将 webView的alpha设置为1

//加载完毕
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [UIView animateWithDuration:0.3 animations:^{
        webView.alpha = 1;
    }];
}

WKWebView禁止放大缩小(捏合手势)

在webView加载完毕之后,运行下面的JS代码禁止WKWebView放大缩小

//加载完毕
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSString *injectionJSString = @"var script = document.createElement('meta');"
    "script.name = 'viewport';"
    "script.content=\"width=device-width, user-scalable=no\";"
    "document.getElementsByTagName('head')[0].appendChild(script);";
    [webView evaluateJavaScript:injectionJSString completionHandler:nil];
}
上一篇下一篇

猜你喜欢

热点阅读