Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change dynamically UIWebView's height depending on HTML content?

I have a simple nib with few buttons and UIWebView on which I want to display some HTML. I want to change the height in UIWebView depending on HTML content. But I can't.

My nib look like this: My nib

My mainViewController.h:

#import <UIKit/UIKit.h>

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

@end

I've used sizeToFit, but it doesn't work. 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");
}

@end

My second question: Why don't the methods webViewDidStartLoad and webViewDidFinishLoad work? Despite I have writen that my controller conforms to UIWebViewDelegate. I'm new in iOS. The height of the WebView doesn't change. And my application looks like this: My app

like image 519
Ibrahim Nikishin Avatar asked Nov 24 '25 01:11

Ibrahim Nikishin


1 Answers

I have found the solution. In my ibecViewController.m I have written method

- (void)viewWillAppear:(BOOL)animated
{
    [[self webV] setDelegate:self];
}

And it works as delegate. My methods webViewDidStartLoad and webViewDidFinishLoad became working too. Then I implemented

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [webV sizeToFit];
}
like image 140
Ibrahim Nikishin Avatar answered Nov 25 '25 18:11

Ibrahim Nikishin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!