如何获取没有导航栏的 UIView 的高度?

发布于 2025-01-07 08:59:21 字数 600 浏览 4 评论 0原文

我试图在一个干净的 UIViewController 中添加一个 UIWebView,它在我的故事板中嵌入导航控制器中:

UIWebView *webView =[[UIWebView alloc] initWithFrame:self.view.bounds)];
webView.delegate = self;
[self.view addSubview:webView];

问题是, self.view.bounds.size.height 是 460,这似乎是整个的高度视图,包括导航栏。我可以减去 self.view.frame.origin.y 的值(20)并获得所需的高度 440,但是有更常见的方法吗?

编辑

请在此处查看我的小代码示例:http://uploads。 demaweb.dk/WebViewTest.zip

当您向下滚动到 UIWebView 的底部时,不会显示网站的最后一部分,因为 webView 太高了。

![在此输入图像描述][1]

I'm trying to add a UIWebView inside a clean UIViewController which in my Storyboard is embedded in a Navigation Controller:

UIWebView *webView =[[UIWebView alloc] initWithFrame:self.view.bounds)];
webView.delegate = self;
[self.view addSubview:webView];

The problem is, that the self.view.bounds.size.height is 460, which seems to be the height of the entire view including the navigation bar. I could subtract the value of self.view.frame.origin.y which is 20 and get the desired hight of 440, but are there a more common way to do this?

EDIT

Please take a look at my small code example here: http://uploads.demaweb.dk/WebViewTest.zip

When you scroll down to the bottom of the UIWebView, the last part of the web site is not shown, as the webView is to high.

![enter image description here][1]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

戏剧牡丹亭 2025-01-14 08:59:22

解决方案

在 UIWebView 中设置 autoresizingMask 解决了我的问题:

self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

Solution

Setting the autoresizingMask in the UIWebView solved my problem:

self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

对你而言 2025-01-14 08:59:22

别担心。导航控制器负责调整其管辖范围内的视图控制器的视图大小。仅当您希望该视图的框架变小以将其他子视图适合其超级视图时,您才可以覆盖该视图的框架。

Don't worry about it. The navigation controller takes care of resizing the view of the view controller under its jurisdiction. You would only override this view's frame if you would like it to be e.g. smaller to fit other subviews onto its superview.

情定在深秋 2025-01-14 08:59:22

默认高度为 480、460 - 没有状态栏的高度。 44px - 导航栏的高度(您可以访问高度,因为它是 UIView - navBar.frame.size.height)。所以,你的视图高度是 480-20-44 = 416。你需要从 self.view.frame.size.height 中减去,而不是 origin.y

Default height is 480, 460 - height without status bar. 44px - height of navigation bar (you can access to height since it's a UIView - navBar.frame.size.height). So, your view height is 480-20-44 = 416. And you need to subtract from self.view.frame.size.height, not origin.y

你的笑 2025-01-14 08:59:22

除了使用 topLayoutGuide (以及可选的 bottomLayoutGuide)之外,我还没有找到更好的解决方案。

也就是说,将视图添加到控制器的 self.view 并设置这些约束:

"H:|[view]|"
"V:[top][view][bottom]"

其中 topself.topLayoutGuidebottom< /code> 是 self.bottomLayoutGuide

作为示例,请参阅 自动布局助手

I haven't found a better solution other than using topLayoutGuide (and optionally bottomLayoutGuide).

That is, adding your view to the controller's self.view and setting these constraints:

"H:|[view]|"
"V:[top][view][bottom]"

Where top is self.topLayoutGuide and bottom is self.bottomLayoutGuide.

As an example, see the configureViewController:fillWithSubView: method in AutolayoutHelper.

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