iOS開發_UIWebView載入本地html

來源:互聯網
上載者:User

標籤:

項目中遇到這樣的問題:webview放在uiscrollview,webview載入本地html,之後需要計算webview高度。

步驟如下:

1.首先建立webview.幾點注意,高度一定要小於你要載入的html的高度,最好設定個最小值,比如1;並且設定scalesPageToFit = YES使之自適應高度;

self.bottomWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.middleView3.frame) + kSectionInterval, kScreenWidth, 100)];    self.bottomWebView.layer.borderColor = self.topView.layer.borderColor;    self.bottomWebView.layer.borderWidth = self.topView.layer.borderWidth;    self.bottomWebView.scalesPageToFit = YES;

 2.其次在控制器中顯示。設定代理,載入h5

self.recordScrollView.bottomWebView.delegate = self;    [self loadH5WithId:self.check.checkId];    [self.view addSubview:self.recordScrollView];

3.載入h5

NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"cj%d.htm", (int)checkId + 1] ofType:nil];    NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];    NSURL *baseUrl = [NSURL fileURLWithPath:path];    [self.recordScrollView.bottomWebView loadHTMLString:htmlString baseURL:baseUrl];

 4.代理方法中計算高度

- (void)webViewDidFinishLoad:(UIWebView *)webView{        float webViewHeight = [[webView stringByEvaluatingJavaScriptFromString: @"document.body.scrollHeight"] floatValue];    webView.frame = CGRectMake(CGRectGetMinX(webView.frame),CGRectGetMinY(webView.frame),CGRectGetWidth(webView.frame),webViewHeight);    webView.scrollView.scrollEnabled = NO;        float heightSum = CGRectGetMinY(webView.frame) + webViewHeight + 15;//15增量    self.recordScrollView.contentSize = CGSizeMake(kScreenWidth, heightSum);    }

 這樣就可以自適應高度了。

 

iOS開發_UIWebView載入本地html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.