iOS使用WKWebView加载HTML5不显示屏幕宽度的问题解决

2020-01-21 07:29:25王振洲


-(WKWebView *)webView {
  if (!_webView) {
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, iPhone5sHeight(375+135*PXSCALEH+285*PXSCALEH), screenW, screenH-50)];
    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
    WKUserContentController *content = [[WKUserContentController alloc]init];
    // 自适应屏幕宽度js
    NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
    WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    // 添加自适应屏幕宽度js调用的方法
    [content addUserScript:wkUserScript];
    wkWebConfig.userContentController = content;

    _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, iPhone5sHeight(375+135*PXSCALEH+285*PXSCALEH), screenW, screenH-50) configuration:wkWebConfig];
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;
  }
  return _webView;
}

到这里适配一下就好了,看效果

iOS,WKWebView,HTML5,屏幕宽度

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU。


注:相关教程知识阅读请移步到IOS开发频道。