2011-01-24 54 views
3

我正在创建一个应用程序,需要一半的页面为web视图,另一半需要填充其他内容。问题是,当我在UI构建器中布局视图时,它的宽度和高度是正确的,但是当我实际加载它时,会填充整个屏幕。iOS中的UIWebView

下面的代码:

- (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]; 
} 

回答

4

你不需要大多数代码。一般而言,您可以在loadView中以编程方式创建视图层次结构,或者使用Interface Builder以图形方式执行此操作。你已经完成了这两项工作 - 你已经在Interface Builder中创建了你的视图,然后把它全部抛弃,并以编程方式再次完成。

完全删除您的loadView方法。这是擦除Interface Builder的工作。默认的loadView实现将反序列化您的笔尖并从中创建您的视图层次结构。

确保您的webView IBOutlet在Interface Builder中连接。这就是你的代码如何引用nib中的内容。当笔尖被反序列化时,它会将您在Interface Builder中放置的UIWebView实例存储在此ivar中。

创建设置网页视图的viewDidLoad方法。一旦你的nib文件被加载并且你的视图层次被构造,这个方法就会被调用,所以你的所有视图都是有效的,你可以做一些事情,比如将HTML加载到web视图中。你会想要这样的事情:

- (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]; 
}