使用 UITabBarController 旋转界面
我使用新的 Xcode 4.2 创建了一个新的“选项卡栏项目”。 使用 UITabBar 的“新”方式是不同的:Xcode 不会创建 xib 文件(使用 UITabBarController),但它通过代码完成所有操作。 好吧,我们开始吧。
所以我在 didFinishLaunchingWithOptions
中的代码是这样的:
UIViewController *viewController1, *viewController2, *viewController3;
UINavigationController *nav1, *nav2, *nav3;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[gemboy_iphone alloc] initWithNibName:@"vc1" bundle:nil];
viewController2 = [[concerti_iphone alloc] initWithNibName:@"vc2" bundle:nil];
viewController3 = [[discografia_iphone alloc] initWithNibName:@"vc3" bundle:nil];
nav1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
nav2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
nav3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
}
else {
//same thing for the iPad version
}
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1, nav2, nav3, nil];
self.window.rootViewController = self.tabBarController;
[self.window addSubview:self.splash.view];
[self.window makeKeyAndVisible];
return YES;
它有效。
我的三个.m文件vc1.m,vc2.m和vc3.m(还有我的iPad UIViewControllers)有这个方法
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
问题是当我旋转iPhone时,它只旋转状态栏,而不旋转TabBarController!
你知道为什么吗? 谢谢
I've created a new "Tab Bar project" with the new Xcode 4.2.
The "new" way to work with UITabBar is different: Xcode doesn't create a xib file (with the UITabBarController), but it does everything via code.
Ok, let's do it.
So my code in didFinishLaunchingWithOptions
is this:
UIViewController *viewController1, *viewController2, *viewController3;
UINavigationController *nav1, *nav2, *nav3;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[gemboy_iphone alloc] initWithNibName:@"vc1" bundle:nil];
viewController2 = [[concerti_iphone alloc] initWithNibName:@"vc2" bundle:nil];
viewController3 = [[discografia_iphone alloc] initWithNibName:@"vc3" bundle:nil];
nav1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
nav2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
nav3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
}
else {
//same thing for the iPad version
}
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1, nav2, nav3, nil];
self.window.rootViewController = self.tabBarController;
[self.window addSubview:self.splash.view];
[self.window makeKeyAndVisible];
return YES;
And it works.
My three .m files vc1.m, vc2.m and vc3.m (and also my iPad UIViewControllers) has this method
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
The problem is that when I rotate the iPhone, it rotates only the status bar, and not the TabBarController!
Do you know why?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您绝对不必也不应该继承 UITabBarController 的子类。
如果标签栏控制器的所有视图控制器都实现了 shouldAutorotateToInterfaceOrientation 并针对相同的方向返回 YES,那么它就会很好地自动旋转。
如果您使用选项卡式应用程序模板在 Xcode 4.2 中创建一个新项目,您将看到它自动旋转得很好。
You absolutely do not have to subclass UITabBarController, nor should you.
A tab bar controller will auto-rotate just fine IF all of its view controllers implement shouldAutorotateToInterfaceOrientation and return YES for the same orientations.
If you create a new project in Xcode 4.2 with the Tabbed Application template, you will see that it auto-rotates just fine.