简体   繁体   English

如何正确使用UINavigationController

[英]How to properly use a UINavigationController

I just recently started programming for iOS/iPhone . 我刚刚开始为iOS/iPhone编程。 I thought I knew what I was doing until XCode5/iOS7 came around. XCode5/iOS7出现之前,我以为我知道自己在做什么。 Previously, I created a class derived from UIViewController with a XIB , added a label, and programatically added it to the rootWindow: 以前,我创建了一个派生自UIViewController的类,带有一个XIB ,添加了一个标签,并以编程方式将其添加到rootWindow:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ... // boilerplate code
    MyViewController* myRoot = [[MyViewController alloc]init];
    self.window.rootViewController = myRoot;

To use a navigation bar, I changed the code slightly: 要使用导航栏,我稍微更改了代码:

MyViewController* myRoot = [[MyViewController alloc]init];
UINavigationController* navigationController = [[UINavigationController alloc]init];
[navigationController pushViewController:myRoot animated:YES];
self.window.rootViewController = navigationController;

This seemed to work fine. 这似乎工作正常。 However, on iOS 7 the controls at the top of my custom view controller appear to be behind the navigation bar. 但是,在iOS 7上,我的自定义视图控制器顶部的控件似乎位于导航栏的后面。 Some googling resulted in this link which describes changes in the status bar. 一些谷歌搜索导致此链接描述状态栏中的更改。

It also seems to indicate that, A) UINavigationController should handle the changes automatically B) "auto layout" should handle the changes automatically, 它似乎也表明,A) UINavigationController应该自动处理更改B)“自动布局”应该自动处理更改,

and that I shouldn't need to worry. 而且我不必担心。 However, my sample app above doesn't appear to handle anything automatically. 但是,我上面的示例应用程序似乎没有自动处理任何内容。

I also found some other sample code which uses the controller differently: adding the navigation controller's view as a subView to an existing view. 我还发现了一些其他使用控制器的示例代码:将导航控制器的视图作为subView视图subView到现有视图中。 This sort of makes sense for adding a navigation controller later on in an app's lifetime, but I am trying to set one up on launch. 这种方式适用于稍后在应用程序生命周期中添加导航控制器,但我试图在启动时设置一个。

Am I using the UINavigationController correctly? 我正确使用UINavigationController吗?

What do I need to consider for iOS7 vs. earlier versions? 对于iOS7与早期版本,我需要考虑什么?

How do I configure "Auto Layout" (I don't see this in interface builder anywhere)? 如何配置“自动布局”(我在界面构建器中没有看到这个)?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM