zoukankan      html  css  js  c++  java
  • iOS之WKWebView加载的网页自适应大小

    一,前言

    有时候在WKWebView加载页面后会发现页面的字会很小, 这是因为原网页没有做手机屏幕尺寸的适配, 那么在后台不做调整的情况下我们移动端怎样来适配页面呢?
    以下代码可以适配大小(原本不可以左右滑动的可能会需要左右滑动才能完整查看)
    

     二,实现方式

    - (WKWebView *)webView {
        if (!_webView) {
            _webView = [[WKWebView alloc] init];
            //以下代码适配大小
            NSString *jScript = @"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';}"
            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;
            
            _webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkWebConfig];
            [self.view addSubview:_webView];
            _webView.navigationDelegate = self;
        }
        return _webView;
    }
    
  • 相关阅读:
    WinDbg常用命令系列---线程相关操作~*
    WinDbg常用命令系列---?*
    使用WinDbg调试入门(内核模式)
    java基础-stringAPI
    springboot-集成WebSockets广播消息
    sprincloud-Feign配置二
    springcloud-Feign配置一
    springboot-集成jdbcTemplate
    spingboot2.x集成单元测试
    springboot跨域CORS处理
  • 原文地址:https://www.cnblogs.com/lxlx1798/p/9874204.html
Copyright © 2011-2022 走看看