Reputation: 31
I am creating an app that requires half of the page to be a webview and the the other half to be filled with other stuff. The problem is that when I layout a view in the UI builder it's the correct width and height but when I actually load it in it fills the entire screen.
Here's the code:
- (void)loadView
{
// Create a custom view hierarchy.
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
UIView *view = [[UIView alloc] initWithFrame:appFrame];
self.view = view;
[view release];
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webView = [[UIWebView alloc] initWithFrame:webFrame];
webView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:webView];
NSString *html = @"<html><head><title>Should be half</title></head><body>I wish the answer were just 42</body></html>";
[webView loadHTMLString:html baseURL:nil];
}
Upvotes: 3
Views: 4652
Reputation: 73936
You don't need most of this code. Generally speaking, you either create your view hierarchy programmatically in loadView
or you use Interface Builder to do it graphically. You've done both - you've created your view in Interface Builder, then thrown it all away and done it again programmatically.
Remove your loadView
method entirely. This is what is wiping out your Interface Builder work. The default loadView
implementation will deserialise your nib and create your view hierarchy from that.
Make sure that your webView
IBOutlet is connected in Interface Builder. This is how your code can reference what's in the nib. When the nib gets deserialised, it will store the UIWebView
instance you laid out in Interface Builder in this ivar.
Create a viewDidLoad
method that sets up the web view. Once your nib file is loaded and your view hierarchy constructed, this method will be called, so all your views will be valid and you can do things like load HTML into the web view. You'll want something like this:
- (void)viewDidLoad
{
NSString *html = @"<html><head><title>Should be half</title></head><body>I wish the answer were just 42</body></html>";
[webView loadHTMLString:html baseURL:nil];
}
Upvotes: 4