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

原文地址:https://www.cnblogs.com/shenlaiyaoshi/p/8676622.html