Programmatically resize a UIViewController based on its container

I have a simple UIViewController with just a UIWebView. UIWebView must take up all available space to show the contents of the URL.

I would like to reuse this ViewController in different places. In some cases, it will be placed in the NavigationController, in some it will be shown as a ModalViewController. Sometimes it can be inside a TabBarController. The size of the UIWebView will vary from case to case.

What is the best way to set up a UIWebView frame without using Interface Builder? In other words, how do I initialize webViewFrame in the following code? Or am I missing something?

- (void)viewDidLoad {
    [super viewDidLoad];
    UIWebView* webView = [[UIWebView alloc] initWithFrame:webViewFrame];
    webView.delegate = self;
    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    [webView loadRequest:request];
    [self.view addSubview:webView];
    [webView release];
}

(self.view.frame, self.navigationController.view.frame ..), , , .

+5
1

NIB, -loadView? :

- (void)loadView
{
    UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    webView.delegate = self;
    self.view = webView
    [webView release];
}

. UIViewController .

, - ( ) , autoresizeMask , .

:

    UIView* parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 200)];
    parentView.autoresizesSubviews = YES;

    UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    [parentView addSubview:webView];
    self.view = parentView;
    [parentView release];

webView parentView.

+8

All Articles