使用tabBar控制器locking到纵向的iOS旋转

目前正在使用标签栏控制器工作的应用程序。 该应用程序将不会旋转到横向模式 – 所有视图inheritance自一个baseVieController,在这里我已经实现了:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return true; } 

现在我知道一个TabBar控制器不会旋转,除非它的所有子视图支持视图试图旋转的方向 – 我的问题是这样的:如果我没有在所有子视图中实现 – (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation方法,是否将这些子视图locking到肖像模式,即使我没有将其指定为所需的方向? 因此将整个tabBar控制器locking为纵向。 我知道以前有类似的问题,但我找不到这个具体问题的答案。 提前致谢。

你可以旋转视图,只需要像下面这样:只需要在你想旋转的视图控制器类中添加代码(这里是“SampleClassName”)

 @interface UITabBarController (rotation) - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; @end @implementation UITabBarController (rotation) - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) { UINavigationController *navController = (UINavigationController *) self.selectedViewController; if ([[navController visibleViewController] isKindOfClass:[SampleClassName class]]) return YES; } return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end 

如果你正在使用故事板开发IOS 5,这将有助于我有同样的问题。 通常在故事板之前,我们可以将TabBarController添加到uview或appdelegate中。 通过故事板,故事板视图并不总是必须连接到视图控制器。

要解决这个问题

在子类字段typesUITabBarController中添加一个新的类文件objective-c类

1 – 在故事板中select标签栏控制器视图

2 – 在自定义类下更改UITabBarController到您新创build的类名称,我叫我的MainTabBarViewController

3 – 在你新创build的类改变这一点

 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } 

 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if (interfaceOrientation == UIInterfaceOrientationPortrait) return YES; if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) return YES; return NO; } 

基本上发生了什么是你在界面生成器中创build一个结构,但只能让你的一部分。 在这种情况下,您仍然必须创build伴随代码。 这一开始我感到困惑,因为我习惯使用.xib从头开始构build视图,并且通常会从appdelegateconfigurationtabbarcontroller。

你也可以有条件地控制每个这样的

 if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) return NO; if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) return NO; if (interfaceOrientation == UIInterfaceOrientationPortrait) return YES; if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) return YES; 

只要返回你想要的是,不要为你不想要的东西返回。 或接受一切返回是的。

至于你的父视图控制器(在你的情况 – baseViewController)实现此方法 –

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return true; } 

您不需要在子视图控制器中实现这一点,因为它支持所有子视图控制器中的所有方向。