iOS 6的标签栏应用程序:shouldAutorotate不工作

问题描述:

我时时刻刻发展与标签栏并且使用的是iOS 6的Xcode 4.5iOS 6的标签栏应用程序:shouldAutorotate不工作

通常情况下,应用程序应该支持所有的接口方向在故事板一些导航视图控制器一个应用程序,但我有两个仅支持纵向模式的视图。

所以我下面的代码添加到视图控制器:

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationPortrait; 
} 

在一个其他应用程序的本人无故事板和导航视图控制器开发了iOS 6的它的工作原理,但她不要! :/

我希望有人能帮助,因为我发现了一些其他的职位,在那里没有什么帮助......

与德国

Laurenz

编辑问候:

我也试过了 - 不起作用! :

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 

} 

乔纳森优秀的答案。

我修改了他的代码来处理导航控制器的一个片段。

- (BOOL)shouldAutorotate { 
    if (self.selectedViewController) { 
     if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) { 
      return [[[(UINavigationController*)self.selectedViewController viewControllers] lastObject] shouldAutorotate]; 
     } 
     return [self.selectedViewController shouldAutorotate]; 
    } else { 
     return YES; 
    } 
} 

- (NSUInteger)supportedInterfaceOrientations { 
    if (self.selectedViewController) { 
     if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) { 
      return [[[(UINavigationController*)self.selectedViewController viewControllers] lastObject] supportedInterfaceOrientations]; 
     } 
     return [self.selectedViewController supportedInterfaceOrientations]; 
    } else { 
     return UIInterfaceOrientationMaskAllButUpsideDown; 
    } 
} 

据我所知,这个问题的产生是由于的UITabBarController UINavigationController的和正在返回自己的缺省值 - (BOOL)shouldAutorotate和 - (NSUInteger)supportedInterfaceOrientations。

一个解决方案是通过类别(或只是子类)扩展这两个类,以便从您自己的视图控制器中实现这些方法的实现中返回适当的值。这是对我工作(你可以拖放到应用程序委托本):

@implementation UITabBarController(AutorotationFromSelectedView) 

- (BOOL)shouldAutorotate { 
    if (self.selectedViewController) { 
     return [self.selectedViewController shouldAutorotate]; 
    } else { 
     return YES; 
    } 
} 

- (NSUInteger)supportedInterfaceOrientations { 
    if (self.selectedViewController) { 
     return [self.selectedViewController supportedInterfaceOrientations]; 
    } else { 
     return UIInterfaceOrientationMaskAllButUpsideDown; 
    } 
} 

@end 

@implementation UINavigationController(AutorotationFromVisibleView) 

- (BOOL)shouldAutorotate { 
    if (self.visibleViewController) { 
     return [self.visibleViewController shouldAutorotate]; 
    } else { 
     return YES; 
    } 
} 

- (NSUInteger)supportedInterfaceOrientations { 
    if (self.visibleViewController) { 
     return [self.visibleViewController supportedInterfaceOrientations]; 
    } else { 
     return UIInterfaceOrientationMaskAllButUpsideDown; 
    } 
} 
@end 

默认情况下,所有的视图控制器将继续自转。在只支持纵向模式的两个视图控制器中,执行以下操作:

-(BOOL)shouldAutorotate { 
    return NO; 
} 

-(NSUInteger)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskPortrait; 
} 
+0

我在appdelegate.m和viewcontroller.m中实现了你的代码。它现在锁定了viewcontroller.m的横向模式,但是当我以横向模式从先前视图到viewcontroller.m时,它将以横向模式显示屏幕,并且这次它不会更改为potrait模式。 – 2013-07-15 10:59:25

+0

这是正确的答案。我一直有这个问题的负荷和分类UITab和UINav固定它。你应该标记这个答案是正确的。 – 2014-02-03 14:33:05

+0

我已经扩展了此代码以支持UISplitViewController:https://gist.github.com/cameroncooke/a9244bc4d677f50940f5 – Camsoft 2015-10-20 08:54:55