iPhone:UINavigationController顶部的奇怪空间

时间:2009-06-24 21:16:13

标签: iphone objective-c cocoa-touch uinavigationcontroller

我在向iPhone应用程序添加UINavigationController时遇到了一个奇怪的问题。我按如下方式添加控制器:

myViewController *viewController = [[myViewController alloc] initWithNibName:@"myView" bundle:nil];

myNavigationViewController *navigationController = [[myNavigationViewController alloc] initWithRootViewController:viewController];

UIView *finalView = myeNavigationViewController.view;

[self.view addSubview:finalView];

所有似乎都按计划工作,除了我在状态栏和UINavigationController标题栏之间的视图顶部有一个奇怪的空白区域。 alt text http://www.andrewskinner.name/problem.png

我在线搜索但不知道要搜索什么。有没有其他人有这个问题?你能指点我一些帮助吗?

提前致谢。

6 个答案:

答案 0 :(得分:11)

该行

UIView *finalView = myeNavigationViewController.view;

添加到代码?这是多余的,因为您可以直接添加视图而不首先将其分配给UIView - 加上它不正确,因为它引用了myNavigationController而不是navigationController ..
我倾向于这样做

myViewController *viewController = [[myViewController alloc] initWithNibName:@"myView" bundle:nil];    
myNavigationViewController *navigationController = [[myNavigationViewController alloc] initWithRootViewController:viewController];
[navigationController.view setFrame: [self.view bounds]];
navigationController.delegate = self;
[self.view addSubview:[navigationController view]];

将框架设置为边界也会删除您询问的顶部的空白区域。

答案 1 :(得分:9)

答案 2 :(得分:2)

问题是UINavigationController理想情况下应该是UIWindow的直接子视图。它将自行定位和调整大小。当您将UINavigationController添加到UIWindow子视图的另一个自定义视图中时,您需要考虑UIWindow中是否显示状态栏来处理此自定义视图的位置和大小。

我的建议是将自定义视图作为UINavigationController的子类:

mySubClass_NavigationController*nav=[[mySubClass_NavigationController alloc] initWithRootViewController:viewController ];

[myUIWindow addSubview:nav.view];

并且在mySubClass_NavigationController中,您可以完成自己现在正在进行的所有自定义(无论控制器是什么)。

答案 3 :(得分:2)

我一直在努力使用非常类似于op的代码,并且在导航控制器上面还有一个白色条。

在UITabController中添加UINavigationController作为视图时出现问题。在我的情况下,空间是由UINavigationController的UINavigationBar部分引起的,考虑到状态栏,它实际上是我试图在UINavigationController中显示的视图的重叠部分。

这是我在其中一个UITabBarController视图控制器中的loadView中最终得到的代码。

SomeUITableViewController *screenList = [[SomeUITableViewController alloc] init];

UINavigationController *navController = [[UINavigationController alloc]
                                         initWithRootViewController:screenList];

CGRect frame = [[navController navigationBar] frame];

frame.origin.y = 0; // Was 20, set to 0 to not take into account the status bar.

[[navController navigationBar] setFrame:frame];

[self setView:[navController view]];

http://discussions.apple.com/message.jspa?messageID=7890362还有更多信息。

答案 4 :(得分:1)

IB中有一个名不见经传的“Hides Bottom Bar on Push”。检查一下。它解决了我的问题。

答案 5 :(得分:0)

也许你以某种方式得到了两个UIViews, 每个都有一个状态栏。检查xib。

相关问题