我最近才开始为 iOS/iPhone
编程。在 XCode5/iOS7
出现之前,我以为我知道自己在做什么。之前,我使用 XIB
创建了一个派生自 UIViewController
的类,添加了一个标签,并以编程方式将其添加到 rootWindow:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
... // boilerplate code
MyViewController* myRoot = [[MyViewController alloc]init];
self.window.rootViewController = myRoot;
为了使用导航栏,我稍微更改了代码:
MyViewController* myRoot = [[MyViewController alloc]init];
UINavigationController* navigationController = [[UINavigationController alloc]init];
[navigationController pushViewController:myRoot animated:YES];
self.window.rootViewController = navigationController;
这似乎工作正常。但是,在 iOS 7
上,我的自定义 View Controller 顶部的控件似乎位于导航栏后面。一些谷歌搜索导致 this link它描述了状态栏的变化。
这似乎也表明,
A) UINavigationController
应该自动处理变化
B) “自动布局”应该自动处理变化,
而且我不需要担心。但是,我上面的示例应用似乎没有自动处理任何事情。
我还发现了一些其他使用 Controller 的示例代码:将导航 Controller 的 View 作为 subView
添加到现有 View 。这对于在应用程序的生命周期后期添加导航 Controller 是有意义的,但我试图在启动时设置一个。
我是否正确使用了 UINavigationController
?
iOS7
与早期版本相比,我需要考虑什么?
如何配置“自动布局”(我在任何地方的界面构建器中都看不到这个)?
最佳答案
- (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;
}
关于ios - 如何正确使用 UINavigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18972720/