Answer the question
In order to leave comments, you need to log in
How to dynamically change height of UIWebView based on HTML content?
I have one simple form. It has a couple of buttons and a UIWebView. I want the UIWebView's height to change depending on the content inside it. The content is unknown in advance.
Nib file looks like this
My mainViewController.h :
#import <UIKit/UIKit.h>
@interface ibecViewController : UIViewController <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webV;
@end
#import "ibecViewController.h"
@interface ibecViewController ()
@end
@implementation ibecViewController
@synthesize webV;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *myHTML = @"<html><body><h1>Hello, world!</h1><p>Hello, world</p><p>Hello, world</p><p>Hello, world</p><p>Hello, world</p><p>Hello, world</p><p>Hello, world</p><p>Hello, world</p><p>Hello, world</p><p>Hello, world</p><p>Hello, world</p><p>Hello, world</p></body></html>";
[webV loadHTMLString:myHTML baseURL:nil];
[webV sizeToFit];
}
- (void)viewDidUnload
{
[self setWebV:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"123");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"456");
}
Answer the question
In order to leave comments, you need to log in
sizeToFit does slightly different things, as far as I understand. Try it through js in the delegate method that reports the end of the download.
stackoverflow.com/questions/3837214/how-to-get-the-content-height-of-a-uiwebview-in-iphone-sdk-objective-c
On the second one. You most likely didn't set a delegate in the storyboard. Look for videos on YouTube on how to do it. The same can be done through code by inserting after [super viewDidLoad];
webV.delegate=self;
Found a solution. If suddenly someone needs it, then here it is:
In the controller, you need to register
- (void)viewWillAppear:(BOOL)animated
{
[[self webV] setDelegate:self];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[webV sizeToFit];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question