无论旋转如何锁定方向

我有一个UITabBar应用程序,其中包含一些视图的嵌入式UINavigation 。 在一个特定的导航视图中,我正在显示图形/图表,最好在landscape显示它们,就像iPhone向左或向右旋转一样。 该应用程序的其余部分更适合肖像。 所以我想“强制”包含图形的视图在横向加载,而不管用户如何物理旋转设备。 我试过了:

 #pragma mark - Rotation -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft); } 

但这似乎没有对视图做任何事情。 如果我为目标选择“支持的界面方向”中的“横向左侧”图标,则它允许整个应用程序在设备旋转时重新定向。 有没有办法以纵向方式锁定我的应用程序以获取所有普通视图,并锁定横向以查看包含图形的视图,以便应用程序忽略实际的设备方向?

首先我使用的是iOS 6 SDK。

我正在使用自定义TabBar控制器。 并通过以下代码集控制TabBar控制器的方向

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // You do not need this method if you are not supporting earlier iOS Versions return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation]; } - (NSUInteger)supportedInterfaceOrientations { return [self.selectedViewController supportedInterfaceOrientations]; } - (BOOL)shouldAutorotate { return YES; } 

并且视图控制器应该包含

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation { return UIInterfaceOrientationIsLandscape(orientation); } 

这就是我所做的,并且能够将视图锁定为横向或纵向(在我的情况下)。 要强制,您可以设置警报视图,说明用户将设备转为横向并向他显示图表。 只是一个建议。

你是对的。 在标签栏应用程序中,不是调用各个视图控制器的shouldAutorotateToInterfaceOrientation :方法。 只调用标签栏控制器的shouldAutorotateToInterfaceOrientation来确定是否以及如何定向视图。

但是,各个视图控制器应该相应地实现shouldAutorotateToInterfaceOrientation 。 当推或拉视图控制器时,这些方法仍用于确定动画效果的方向。

我自己从未尝试过以下内容:您可以尝试子标记选项卡栏控制器并相应地响应shouldAutorotateToInterfaceOrientation ,具体取决于当前向用户显示的视图。 但是我担心Apple有充分的理由强迫我们在标签栏应用程序中为所有视图支持相同的方向。

尝试使用横向模式中需要的ViewController

 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight ); }