3

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

Cezar
  • 55,636
  • 19
  • 86
  • 87

2 Answers2

1

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];
}
Venk
  • 5,949
  • 9
  • 41
  • 52
  • I also saw a suggestion(don't remember where) that injecting JS for the onload event to notify back to obj-c work better, as , supposedly, webViewDidFinishLoad sometimes forgets to be fired. Not sure about the claims though, but they are sound. Better safe than sorry :) – Mihai Timar Sep 24 '13 at 17:17
0

Answer to First Question:

Try using [webV setScalesPageToFit:YES]; function instead of sizeToFit. Html content will autoresize to webview size. I guess this function is only present in higher versions of ios.

Answer to Second Question: You must delegate the webview object with the self object representing the view controller (ibecviewcontroller) inside which webview was defined. After delegation is done then you can probably make use of the delegate functions. If you are doing it through Xib then you should connect the webview object which is in this case webV with the Files Owner object or icon in IB.

Hope that helps..

m4n1c
  • 127
  • 8
  • Thank you for your answers. But the first not what I wanted. Using setScalesPageToFit:YES doesn't change my WebView, but changes HTML scale. And it became smaller. For the second I have written method (void)viewWillAppear:(BOOL)animated { [[self webV] setDelegate:self]; } And - (void)viewWillAppear:(BOOL)animated { [[self webV] setDelegate:self]; } – Ibrahim Nikishin Oct 11 '12 at 08:03