使用 UITabBarController 旋转界面

发布于 2024-12-12 10:29:33 字数 1566 浏览 0 评论 0原文

我使用新的 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 技术交流群。

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

发布评论

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

评论(1

赠我空喜 2024-12-19 10:29:33

您绝对不必也不应该继承 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.

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