根据容器以编程方式调整 UIViewController 的大小
我有一个简单的 UIViewController 和一个 UIWebView。 UIWebView 应占用所有可用空间来显示 url 的内容。
我想在不同的地方重用这个 ViewController。在某些情况下,它将被推送到 NavigationController 中,在某些情况下,它将显示为 ModalViewController。有时它可能位于 TabBarController 内。 UIWebView 的大小因情况而异。
不使用 Interface Builder 设置 UIWebView 框架的最佳方法是什么?换句话说,我应该如何在下面的代码中初始化webViewFrame?还是我还缺少其他东西?
- (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 等),但似乎没有任何方法适用于所有情况。
I have a simple UIViewController with just a UIWebView. The UIWebView should take all available space to show the content of an url.
I would like to reuse this ViewController in various places. In some cases it will be pushed into a NavigationController, in some it will be shown as a ModalViewController. Sometimes it might be inside a TabBarController. The size of the UIWebView will vary from case to case.
What's the best way to set the frame of the UIWebView without using the Interface Builder? In other words, how should I initialize webViewFrame in the following code? Or is there something else I'm missing?
- (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];
}
I have tried with different possibilities (self.view.frame, self.navigationController.view.frame, etc.), but nothing seems to work for all cases.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不使用 NIB,那么您的 -loadView 方法是什么样的?它应该看起来像这样:
在这种情况下,初始框架是无关紧要的。 UIViewController 将负责正确调整视图的大小。
但是,如果您确实想要将 Web 视图(或任何其他视图)插入到父视图中,则可以使用 autoresizeMask 来控制视图如何随其父视图调整大小。
例如:
在这种情况下,webView的初始框架是相对于parentView的边界的。
If you're not using a NIB then what does your -loadView method look like? It should look like this:
In this case, the initial frame is irrelevant. The UIViewController will take care of sizing your view correctly.
However, if you actually want to insert a web view (or any other view) into a parent view, you use autoresizeMask to control how your view resizes with its parent.
For example:
In this case, the webView's initial frame is relative to the parentView's bounds.