美文网首页
iOS WKWebView 网页禁止缩放

iOS WKWebView 网页禁止缩放

作者: ios_暗夜行者 | 来源:发表于2021-08-25 09:53 被阅读0次

    -(WKWebView*)baseWkWebView

    {

        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];

           WKUserContentController *wkUController = [[WKUserContentController alloc] init];

        wkWebConfig.allowsInlineMediaPlayback = YES;

        wkWebConfig.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;

        WKPreferences *preferences = [WKPreferences new];

        preferences.javaScriptCanOpenWindowsAutomatically = YES;

        wkWebConfig.preferences= preferences;

         // var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}

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

        // 自适应屏幕宽度js

        //NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}";

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

        //禁止网页缩放

        NSString *jSString = @"var script = document.createElement('script');"

        "script.type = 'text/javascript';"

        "script.text = \"function ResizeImages() { "

        "var myimg,oldwidth;"

        "var maxwidth = %f;"

        "for(i=0;i"

        "myimg = document.images[i];"

        "if(myimg.width > maxwidth){"

        "oldwidth = myimg.width;"

        "myimg.width = %f;"

        "}"

        "}"

        "}\";"

        "document.getElementsByTagName('head')[0].appendChild(script);";

           WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];

           // 添加js调用

           [wkUControlleraddUserScript:wkUserScript];

        wkWebConfig.userContentController= wkUController;

        if (!_baseWkWebView)

        {

            _baseWkWebView=[[WKWebView alloc]initWithFrame:CGRectMake(0, Height_NavBar, SCREEN_WIDTH, SCREEN_HEIGHT-Height_NavBar) configuration:wkWebConfig];

            _baseWkWebView.scrollView.bounces = NO;

            _baseWkWebView.opaque = NO;

           // _baseWkWebView.UIDelegate = self;

           // _baseWkWebView.navigationDelegate = self;

            [_baseWkWebView sizeToFit];

         }

        return _baseWkWebView;

    }

    相关文章

      网友评论

          本文标题:iOS WKWebView 网页禁止缩放

          本文链接:https://www.haomeiwen.com/subject/dtssiltx.html