iPhone UINavigationController 出现问题(UINavigationBar 在错误的位置)

发布于 2024-11-08 02:42:19 字数 1630 浏览 0 评论 0原文

我正在对应用程序进行一些调整,包括更改为基于导航的界面。作为该更改的一部分,我编写了一个包含 UINavigationController 的视图控制器。问题是,由于某种奇怪的原因,由 UINavigationController 管理的 UINavigationBarUIToolbar 与应有的位置向下移动了 20px。我设法生成了以下示例来演示该问题:

// MyAppDelegate.m

@implementation MyAppDelegate
@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.frame = [[UIScreen mainScreen] applicationFrame];
    [self.window makeKeyAndVisible];

    TestController* tc = [TestController new];
    [self.window addSubview:tc.view];

    return YES;
}

@end

// TestController.m

@implementation TestController

- (void)loadView
{
    self.view = [[UIView alloc] initWithFrame:CGRectZero];

    UINavigationController* navController = [UINavigationController new];
    navController.view.backgroundColor = [UIColor blueColor];
    [navController setToolbarHidden:NO animated:NO];

    [self.view addSubview:navController.view];
}

@end

这会在我的计算机上产生以下结果:

Shot of example

正如您所看到的,控件比我预期的位置低了 20 像素。我已经尝试了几乎所有我能想到的东西(wantsFullScreenLayoutautoresizesSubviews等的各种组合),但没有任何积极效果。这也与以编程方式弄乱状态栏无关(就像我遇到的大多数其他示例中的情况一样),因为我在任何时候都不会弄乱状态栏。无论导航控制器中有或没有根视图控制器,都会发生这种情况 - 如果有根视图控制器,它的内容也会向下移动 20px(因此它们实际上位于相对于导航栏和工具栏的正确位置)。

非常感谢任何帮助!

编辑:经过一番调查后,似乎删除行 self.window.frame = [[UIScreen mainScreen] applicationFrame]; 似乎可以纠正导航的定位栏、工具栏和内容。也就是说,现在应用程序中的一些其他视图位于错误的位置(在状态栏下方)。我的理解是,通常建议使用这条线来确保窗口的大小正确?

I'm in the process of making some adjustments to an app, including changing to a navigation-based interface. As part of that change I've written a view controller that contains a UINavigationController. The problem is, for some strange reason the UINavigationBar and UIToolbar managed by the UINavigationController are displaced 20px down from where they should be. I've managed to produce the following example that demonstrates the issue:

// MyAppDelegate.m

@implementation MyAppDelegate
@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.frame = [[UIScreen mainScreen] applicationFrame];
    [self.window makeKeyAndVisible];

    TestController* tc = [TestController new];
    [self.window addSubview:tc.view];

    return YES;
}

@end

// TestController.m

@implementation TestController

- (void)loadView
{
    self.view = [[UIView alloc] initWithFrame:CGRectZero];

    UINavigationController* navController = [UINavigationController new];
    navController.view.backgroundColor = [UIColor blueColor];
    [navController setToolbarHidden:NO animated:NO];

    [self.view addSubview:navController.view];
}

@end

This produces the following result on my machine:

Shot of example

As you can see, the controls are 20px down from where I'd expect them to be. I've tried just about everything I can think of (various combinations of wantsFullScreenLayout, autoresizesSubviews, etc) with no positive effect. This also has nothing to do with programatically messing with the statusbar (as seems to be the case in most other examples of this I have come across), since I do not at any point mess with the statusbar. This occurs with or without a root view controller in the navigation controller - if there is one, it's contents are shifted 20px down too (so they actually are in the right place relative to the navigation bar and toolbar).

Any help much appreciated!

EDIT: After a bit of investigation, it seems that removing the line self.window.frame = [[UIScreen mainScreen] applicationFrame]; seems to correct the positioning of the navigation bar and toolbar and content. That said, now some other views in the application are in the wrong place (up underneath the statusbar). My understanding is that line is generally recommended to ensure that the window is the correct size?

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

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

发布评论

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

评论(2

在梵高的星空下 2024-11-15 02:42:19

正如我在编辑中提到的,删除行 self.window.frame = [[UIScreen mainScreen] applicationFrame]; 似乎已经纠正了我 95% 的问题。我已经设法通过对窗口使用相同的背景颜色和有问题的其余视图来捏造一种方法来修复其他 5%,但我不能说我对这个解决方案感到兴奋 - 我不应该这样做这样做。

我会继续尝试,如果我找到更好的结果,我肯定会在这里发布编辑。

As mentioned in my edit, removing the line self.window.frame = [[UIScreen mainScreen] applicationFrame]; seems to have corrected 95% of my problems. I've managed to fudge an approach to fix the other 5% by using the same background colour for my window and the remaining views having issues, but I can't say I'm thrilled with this solution - I shouldn't have to do that.

I'll keep experimenting, and if I find a better result will certainly post an edit here.

失退 2024-11-15 02:42:19

UINavigationController 不能很好地用作子视图;正如您所注意到的,即使状态栏实际上不在状态栏下方,它通常也会为状态栏留出空间。如果您不想编写自己的容器视图控制器,则应该重新编写代码,根本不将视图控制器的视图添加为子视图。

也就是说,我很幸运地通过在 UINavigationController 上将 wantsFullScreenLayout 设置为 NO 来修复它,这将使其不为状态栏留下空间。当然,您希望在分配它之后、在 loadView 被触发之前执行此操作。

UINavigationController does not play nicely with being used as a subview; as you've noticed, it will often leave room for the status bar even when it is not actually under the status bar. If you're not trying to write your own container view controller, you should rework your code to not be adding a view controller's view as a subview at all.

That said, I've had luck fixing it by setting wantsFullScreenLayout to NO on the UINavigationController, which will make it not leave space for the status bar. You would, of course, want to do this just after allocating it, before loadView gets triggered.

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