zoukankan      html  css  js  c++  java
  • ios -WKWebView 高度 准确,留有空白的解决方案

     

    #import "ViewController.h"

    #import <WebKit/WebKit.h> 

    @interface ViewController ()<WKNavigationDelegate>

    @property (nonatomic, strong)WKWebView *webView;

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad

    {

        _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 1)];

        _webView.UIDelegate = self;

        _webView.navigationDelegate = self;

        [self.view addSubview:_webView];

        NSString *url = [NSString stringWithFormat:@"https://mdetail.tmall.com/templates/pages/desc?id=13128565333"];

        NSLog(@"dfurk:%@",url);

        [self.webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]]];

     

        [self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];

    }

     

    - (void)dealloc {

        

        [self.webView.scrollView removeObserver:self forKeyPath:@"contentSize" context:nil];

    }

     

    - (void)observeValueForKeyPath:(NSString *)keyPath

                          ofObject:(id)object

                            change:(NSDictionary *)change

                           context:(void *)context

    {

        if (object == self.webView.scrollView && [keyPath isEqual:@"contentSize"]) {

            // we are here because the contentSize of the WebView's scrollview changed.

            

            UIScrollView *scrollView = self.webView.scrollView;

            self.webView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);

            NSLog(@"New contentSize: %f x %f", scrollView.contentSize.width, scrollView.contentSize.height);

        }

    }

    /*

    #pragma mark - Navigation

     

    // In a storyboard-based application, you will often want to do a little preparation before navigation

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        // Get the new view controller using [segue destinationViewController].

        // Pass the selected object to the new view controller.

    }

    */

     

    @end

  • 相关阅读:
    22.json&pickle&shelve
    22.BASE_DIR,os,sys
    21.time和random
    21.模块的执行以及__name__
    21.python的模块(Module)和包(Package)
    21. 对文件进行查询修改等操作
    20.装饰器和函数闭包
    19.python基础试题(三)
    19.生产者消费者模型
    19.yield和send的区别
  • 原文地址:https://www.cnblogs.com/shenlaiyaoshi/p/8676622.html
Copyright © 2011-2022 走看看