1. 程式人生 > >WKWebView內容自適應寬度

WKWebView內容自適應寬度

  NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";

    WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    WKUserContentController *wkUController
= [[WKUserContentController alloc] init]; [wkUController addUserScript:wkUScript]; WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init]; wkWebConfig.userContentController = wkUController; // 建立一個view來控制scrollview的contentsize self.wk = [[WKWebView alloc] initWithFrame:CGRectMake(10, 0, Main_Screen_Width - 20, 200) configuration:wkWebConfig];