为什么我的一些 UIViews 在导航后发生了变化? [英] How come some of my UIViews are shifted after navigation?

查看:33
本文介绍了为什么我的一些 UIViews 在导航后发生了变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一些应用程序设计中或仅针对一些 UIViews,在 navigationController 的 pushViewController 之后,我的新视图将移出窗口的状态栏高度.因此,我会将这段代码存根放在 viewDidLoad 方法中.

In some of my application designs or for just some UIViews, following a navigationController's pushViewController, my new view will be shifted off the window by the height of the status bar. As a result, I will put this code stub in the viewDidLoad method.

CGRect  frameAt = [self.view frame];
CGRect  statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
frameAt.origin.y += statusBarFrame.size.height;
[self.view setFrame: frameAt];

我认为这是 XCode 和 Interface Builder 的意图是没有意义的,所以我怀疑我在视图设计过程中对 SDK 做了一些根本性的错误.此外,在我不必改变观点的极少数情况下,我真的不知道这两种设计方法有什么区别.

It does not make sense to me that this is the intention of XCode and Interface Builder, so I suspect that I am doing something fundamentally wrong with the SDK during my view design. Furthermore, on the rare occasion that I don't have to shift my view, I really don't know what the difference in the two design approaches.

另请注意,大部分时间我都尝试使用 IB 来设计我的视图,并进行一些小的自定义.

Note also, that most of the time I try to design my views using IB, with some minor customization.

有没有其他人遇到过这个问题并且知道他们在没有这样的代码存根的情况下如何修复?

Does anyone else run into this and know what they do to fix without such a code stub?

推荐答案

我用过 Apple 的 NavBar 示例代码尝试重现此问题.

I've used Apple's NavBar sample code to try and reproduce this problem.

applicationDidFinishLaunching 最初是这样实现的:

the applicationDidFinishLaunching is originally implemented like this:

[window addSubview:navigationController.view];
[window makeKeyAndVisible];

如果我改成这样:

UIViewController *shellController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
[shellController.view addSubview:navigationController.view];
[window addSubview:shellController.view];
[window makeKeyAndVisible];

然后我发现了差距.

但是,如果我只这样做:

However if I only do this:

UIView *shell = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[shell addSubview:navigationController.view];
[window addSubview:shell];
[window makeKeyAndVisible];

然后一切看起来都很正常.

Then everything looks normal.

所以我猜如果它是根视图控制器,每个视图控制器都会偏移它的视图.但是导航控制器会覆盖此行为以偏移其视图,无论它是否是根视图控制器.因此,我会说您的差距出现是因为您在层次结构中的某个位置有一个导航控制器,而不是它应有的位置.

So I guess every view controller will offset its view if its the root view controller. But the navigation controller overrides this behaviour to offset its view regardless of whether it's the root view controller. Therefore I would say your gap is appearing because you've got a navigation controller somewhere lower in the hierarchy than it's supposed to be.

这篇关于为什么我的一些 UIViews 在导航后发生了变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆