根据容器以编程方式调整 UIViewController 的大小

发布于 2024-08-13 03:10:57 字数 826 浏览 3 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2024-08-20 03:10:57

如果您不使用 NIB,那么您的 -loadView 方法是什么样的?它应该看起来像这样:

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

在这种情况下,初始框架是无关紧要的。 UIViewController 将负责正确调整视图的大小。

但是,如果您确实想要将 Web 视图(或任何其他视图)插入到父视图中,则可以使用 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的边界的。

If you're not using a NIB then what does your -loadView method look like? It should look like this:

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

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:

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

In this case, the webView's initial frame is relative to the parentView's bounds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文