iOS development _uiwebview loading local HTML

Source: Internet
Author: User

This problem is encountered in the project: WebView load the local HTML Uiscrollview,webview, and then you need to calculate the webview height.

The steps are as follows:

1. First create the WebView. Note that the height must be less than the height of the HTML you want to load, preferably set a minimum value, such as 1, and set scalespagetofit = yes to make it adaptive height;

Self.bottomwebview = [[UIWebView alloc]initwithframe:cgrectmake (0, Cgrectgetmaxy (self.middleView3.frame) + Ksectioninterval, Kscreenwidth,)];    Self.bottomWebView.layer.borderColor = Self.topView.layer.borderColor;    Self.bottomWebView.layer.borderWidth = Self.topView.layer.borderWidth;    Self.bottomWebView.scalesPageToFit = YES;

2. The second is displayed in the controller. Set up the agent, load H5

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

3. Load 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. Calculate Height in Agent method

-(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 increment    self.recordScrollView.contentSize = Cgsizemake (Kscreenwidth, heightsum);    }

This allows you to adapt to the height of your own.

iOS development _uiwebview loading local HTML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.