序列图像板中具有不同界面方向首选项的多个UIViewController



我已经阅读了关于同一问题的其他几个问题(和答案),但无法为我的案例找到一个有效的解决方案。

我正在使用Storyboards为iOS7开发一个iOS应用程序。此应用程序由多个UIViewController组成。除一个外,所有这些必须仅以纵向界面方向显示。我的问题是,所有的视图都在不该旋转的时候旋转。

在XCode项目设置中,我勾选了PortraitLandscape Left设备方向。

相关的Apple文档位于以下URL:Controlling What Interface Orientations are Supported(iOS 6)、supportedInterfaceOrientations和preferredInterface OrientationForPresentation。

我在Storyboad中的所有UIViewControllers(但可以旋转的除外)添加了以下方法:

- (BOOL) shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}

不幸的是,所有视图都旋转到左横向位置。为什么?

对于纵向,您需要向方法- (BOOL) shouldAutorotate添加更多代码

- (BOOL)shouldAutorotate{
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) {
return YES;
}
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(@"rotate");
}

你需要在你的应用程序中添加这种方法

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  // iOS 6 autorotation fix {
return UIInterfaceOrientationMaskAll;
}

从这里得到解决方案:iOS6:supportedInterfaceOrientations不工作(已调用,但接口仍在旋转)。

在写我的问题之前,我没有发现那个问题。很抱歉。

问题出在Apple文档中。他们在一个地方写道:

当视图控制器显示在根视图控制器上时系统行为有两种变化。首先,呈现的视图当确定是否支持方向。

您认为由占据屏幕的UIViewController做出决定的地方。

在UIViewController类引用中,他们写道:

通常,系统仅在根视图上调用此方法窗口的控制器或视图控制器,以填充整个屏幕;子视图控制器使用窗口的部分由其父视图控制器为其提供,不再直接参与关于支持哪些轮换的决策。应用程序的方向遮罩和视图的交叉点控制器的方向掩码用于确定哪些方向可以将视图控制器旋转到。

您应该明白,使用UINavitationControlroller,第一句话不适用。

确保项目中启用了纵向、横向右侧和横向左侧。然后,如果你想阻止特定视图的某些方向:

– application:supportedInterfaceOrientationsForWindow:

如果您想强制特定视图的方向,请检查问题"如何在iOS 6中处理不同的方向"。请参阅那里的答案,以获取您所需要的项目示例。

基本上,您需要在视图控制器(您想要旋转的视图控制器)中嵌入一个自定义导航控制器。在这个自定义导航控制器中添加以下方法

- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}

并添加到应该旋转的视图控制器:

- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

最新更新