UIWebView Adaptive Height Example of iOS development

Source: Internet
Author: User

UIWebView when you can not set a fixed height, you need to be based on the content of the Web page to adapt to the height, the reference code is as follows:

//
Viewcontroller.m
UIWebView highly Adaptive
//
Created by Mac on 16/3/18.
COPYRIGHT©2016 year Zmit. All rights reserved.
//

#import "ViewController.h"

@interface Viewcontroller () <uiscrollviewdelegate,uiwebviewdelegate>{
UIWebView *mywebview;
}

@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];
Do no additional setup after loading the view, typically from a nib.
Self.view.backgroundColor = [Uicolor Graycolor];
Mywebview = [[UIWebView alloc] Initwithframe:cgrectmake (0, [UIScreen mainscreen].bounds.size.width, 30)];
Mywebview.delegate = self;
MyWebView.scrollView.delegate = self;
Mywebview.scalespagetofit = yes;//Automatically scales the page to fit the screen
[Self.view Addsubview:mywebview];

Nsurl *url = [Nsurl urlwithstring:@ "http://www.111cn.net/"];
Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];
[Mywebview Loadrequest:request];
}
#pragma mark-Agent method
-(void) Webviewdidfinishload: (UIWebView *) webview{
CGFloat webviewheight = [[WebView stringbyevaluatingjavascriptfromstring:@ "Document.body.offsetHeight"]floatvalue] ;
CGRect newframe = webview.frame;
NewFrame.size.height = Webviewheight;
Webview.frame = Newframe;
Reset the WebView contentsize
MyWebView.scrollView.contentSize = newframe.size;
If there are other controls underneath the WebView block, you can set its frame here by Mywebview
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any of the can is recreated.
}

@end

Related Article

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.