I
I
IbrahimKZ2012-10-11 09:23:05
iOS
IbrahimKZ, 2012-10-11 09:23:05

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_nib.png
My mainViewController.h :

#import <UIKit/UIKit.h>

@interface ibecViewController : UIViewController <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webV;

@end

I used sizeToFit but it didn't help. My mainViewController.m :
#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");
}

And the second question. Why don't the webViewDidStartLoad and webViewDidFinishLoad methods work even though I specified that the controller conforms to the UIWebViewDelegate protocol ?
I'm new to iOS, so don't judge too harshly if I make stupid mistakes.
The application looks like this:
iphone.png
You can see that the height of the UIWebView has not changed.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
An, 2012-10-11
@Flanker_4

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;

I
IbrahimKZ, 2012-10-11
@IbrahimKZ

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];
}

Then the webViewDidStartLoad and webViewDidFinishLoad methods will work. Accordingly, it will be possible to use [webV sizeToFit]; in webViewDidFinishLoad :
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [webV sizeToFit];
}

This way the UIWebView is only resized after loading. And these methods can only be called if our controller complies with the UIWebViewDelegate protocol

S
s0L, 2012-10-12
@s0L

webV.scrollView.contentSize

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question