1. 程式人生 > >iOS 計算webview高度

iOS 計算webview高度

#import "SubjectViewController.h"@interface SubjectViewController ()@property(nonatomic, strong)UIWebView *webView;@[email protected] SubjectViewController- (void)viewDidLoad { [super viewDidLoad]; self.webView = [self createWeb]; [self.view addSubview:self.webView] ; NSString *htmlString = [NSString stringWithFormat:@"%@", self.info[@"question_html"]]; [self loadHTML:htmlString];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];}- (void)unloadData { self.webView = nil;}- (void)loadHTML:(NSString *)html { [self.webView loadHTMLString:html baseURL:nil];}- (UIWebView *)createWeb { UIWebView *web =[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)]; web.delegate = self; web.scalesPageToFit = NO; [web.scrollView setShowsHorizontalScrollIndicator:NO]; [web.scrollView setShowsVerticalScrollIndicator:NO]; return web;}- (void)webViewDidFinishLoad:(UIWebView *)webView{ NSString *htmlString = [NSString stringWithFormat:@"%@", self.info[@"question_html"]]; DDLog(@"%@",htmlString); CGFloat documentHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue]; DDLog(@"%f", documentHeight); self.webView.frame = CGRectMake(0, 0, kScreenWidth, documentHeight); //document.body.scrollHeight}@end