为什么我必须减去 UINavigationBar 的高度两次才能使 UIWebView 达到正确的大小?

发布于 2024-10-08 16:30:35 字数 1109 浏览 4 评论 0原文

我在我的应用程序中使用 UINavigationBar 和三个不同的 UIViewController。当 RootViewController 的视图位于堆栈顶部时,通过在 RootViewController.m 中实现以下内容来隐藏导航栏:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[self.navigationController setNavigationBarHidden:TRUE animated:TRUE];}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

[self.navigationController setNavigationBarHidden:FALSE animated:TRUE];}

firstViewController 被推到顶部时,一切正常。在IB中,我告诉这个视图它有一个状态栏和一个导航栏。它的所有子视图(在 IB 中添加)都按照模拟器和设备上的预期进行布局。

然而,当 secondaryViewContoller 被推到顶部时,事情就变得奇怪了。在IB中,我告诉视图,和以前一样有一个状态栏和一个导航栏。我添加了一个 UIWebView 来填充屏幕的其余部分(在 IB 中验证尺寸为 320 x 416),并且在 IB 中一切看起来都是正确的。我在模拟器和我的设备上运行该应用程序,UIWebView 在导航栏后面运行,就好像它是 320 x 460 一样。

为了解决这个问题,我将此代码添加到 SecondViewController.m:

- (void)viewDidLoad {
[super viewDidLoad];

[webView setFrame:CGRectMake(0.0, 44.0, 320.0, 372.0)]; }

现在运行时一切看起来都很好模拟器和设备。但这是不对的!高度确实是 416,而不是 372。为什么我必须减去 UINavigationBar 的高度两次?我的黑客有效,但我认为我在这里缺少一些重要的东西,我确实需要理解。任何想法都将不胜感激。

I am using a UINavigationBar in my app with three different UIViewControllers. The RootViewController hides the navigation bar when its view is on top of the stack by implementing the following in RootViewController.m:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[self.navigationController setNavigationBarHidden:TRUE animated:TRUE];}

and

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

[self.navigationController setNavigationBarHidden:FALSE animated:TRUE];}

When firstViewController is pushed to the top, everything works just fine. In the IB, I told this view that it has a status bar and a navigation bar. All of its subviews (added in the IB) are laid out just as expected on the simulator and on the device.

However, when secondViewContoller is pushed to the top, things get weird. In the IB, I told the view that there was a status bar and a navigation bar just as before. I added a UIWebView to fill the remainder of the screen (verified dimensions of 320 x 416 in the IB) and it all looks correct in the IB. I ran the app on the simulator and my device, and the UIWebView runs behind the navigation bar as if it were 320 x 460.

To fix this problem, I added this code to SecondViewController.m:

- (void)viewDidLoad {
[super viewDidLoad];

[webView setFrame:CGRectMake(0.0, 44.0, 320.0, 372.0)]; }

And now it all looks fine when running on the simulator and the device. But this isn't right! The height really is 416, not 372. Why did I have to subtract the height of the UINavigationBar twice? My hack works, but I think there is something important that I'm missing here that I really need to understand. Any ideas are greatly appreciated.

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

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

发布评论

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

评论(1

情泪▽动烟 2024-10-15 16:30:35

不完全确定为什么你必须减去高度两次,但我可以解释为什么你必须这样做。

基本上,在 viewDidLoad 中,您的视图控制器及其关联的视图不知道它们将在哪里使用。这意味着您的视图当前没有超级视图。如果您在视图控制器以模态方式呈现、添加到导航堆栈或在选项卡栏中呈现之前设置框架,则视图可能会移动、调整大小以及各种疯狂的东西。这个问题的答案是在 viewWillAppear: 中执行布局:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    webView.frame = self.view.bounds;
}

该代码片段将调整 Web 视图以填充视图控制器占据的区域,并将在屏幕上呈现视图之前立即执行。当它执行时,self.view 已经被调整以适应导航栏、选项卡栏、状态栏等。

Not entirely sure why you're having to subtract the height twice, but I can explain why you have to do it at all.

Basically, in viewDidLoad, your view controller and its associated view have no idea where they'll be used. This means that your view currently has no superview. If you set your frame before the view controller is presented modally, added to a navigation stack, or presented in a tab bar, the view is liable to get moved around and resized and all kinds of crazy stuff. The answer to this problem is to perform layout in viewWillAppear: instead:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    webView.frame = self.view.bounds;
}

That snippet of code will adjust the web view to fill the area taken by the view controller, and will execute immediately before presenting the view on screen. When it executes, self.view has already been adjusted to account for a navigation bar, tab bar, status bar, etc.

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