Reputation: 41
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
Upvotes: 3
Views: 1027
Reputation: 41
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];
}
Upvotes: 1
Reputation: 127
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..
Upvotes: 0