【问题标题】:io6 allow interface orientation based on some conditionio6 允许基于某些条件的界面方向
【发布时间】:2013-08-19 12:02:51
【问题描述】:

我有一个标志,用户将从设置中设置它。如果设置为否,则仅允许纵向。如果标志为 YES,那么纵向和横向都将被允许。 在 ios 5 及以下版本中

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    //[image_signature setImage:[self resizeImage:image_signature.image]];
    if(flag == YES)
        return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationPortrait);
    else
        return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

但在 ios 6 及更高版本中已弃用上述方法。 我在尝试

-(BOOL)shouldAutorotate 
- (NSUInteger)supportedInterfaceOrientations 

但没有成功。 请帮帮我。

编辑 我试过了。

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    NSLog(@"preferredInterfaceOrientationForPresentation");
    return UIInterfaceOrientationMaskPortrait;
}

- (NSUInteger)supportedInterfaceOrientations
{
    NSLog(@"supportedInterfaceOrientations");
    return UIInterfaceOrientationMaskPortrait;
}

但只有supportedInterfaceOrientations 只被调用一次。当我改变模拟器的方向时,两种方法都没有调用。

【问题讨论】:

  • 你没有删除 -(BOOL)shouldAutorotate 方法吗?
  • 没有。 shouldAutorotate 返回 YES。
  • 您的新代码显示您正在使用“UIInterfaceOrientationMaskPortrait”作为preferredInterfaceOrientationForPresentation 方法,而应该使用UIInterfaceOrientationPortrait。只有“supportedInterfaceOrientations”方法应该使用“mask”版本。如果标志为 YES,我以为你想要横向和纵向?
  • 在 appdelegate 中,我将 rootView 设置为导航控制器。这就是它不打电话的原因。我将 viewController 设置为 rootViewController。有效。但我需要导航控制器作为 rootViewController。
  • 使用额外的应用委托方法查看我的更新答案。

标签: ios orientation


【解决方案1】:
shouldAutorotateToInterfaceOrientation:
Returns a Boolean value indicating whether the view controller supports the specified orientation. 
(Deprecated in iOS 6.0. Override the supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation methods instead.)

(http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html)

所以您可以在较新版本中使用的两种方法是:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/preferredInterfaceOrientationForPresentation

- (NSUInteger)supportedInterfaceOrientations

此方法使用以下位掩码

UIInterfaceOrientationMaskPortrait

UIInterfaceOrientationMaskLandscapeLeft

UIInterfaceOrientationMaskLandscapeRight

UIInterfaceOrientationMaskPortraitUpsideDown

UIInterfaceOrientationMaskLandscape

UIInterfaceOrientationMaskAll

UIInterfaceOrientationMaskAllButUpsideDown

http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/supportedInterfaceOrientations

更新:

好的,我不确定为什么它不适合你。

我刚刚创建了一个演示应用来复制您正在尝试做的事情:

使用右键>复制图片网址并在新的浏览器标签中打开以查看更大的图片

当方向标志为NO时,按钮文本显示“Landscape Denied”,并且在模拟器中旋转设备不会导致界面旋转,如预期:

但是,在单击按钮以允许横向方向后,旋转模拟器设备实际上会改变界面的方向,正如预期的那样:

您在另一个根视图控制器中没有任何其他方向委托方法覆盖当前视图控制器的方向委托方法吗?

另外,我不知道使用自动布局是否会干扰,我倾向于不使用它。在我的 XIB 文件中的视图中,我选择了“视图”对象,然后在检查器中,我取消勾选Use Auto Layout”。我上面的示例项目截图没有使用自动布局。

更新 2:

好的,我发现这个解决方案可以将导航控制器用作窗口的根视图控制器(把它放在你的 AppDelegate.m 中):

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations =UIInterfaceOrientationMaskAllButUpsideDown;

    if(self.window.rootViewController)
    {
        UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presentedViewController supportedInterfaceOrientations];
    }
    return orientations;
}

在这篇 Stackoverflow 帖子中:

iOS 6 AutoRotate In UiNavigationController

带有导航控制器的新屏幕截图:

【讨论】:

  • 在目标中我允许了 portrat、landscapleft 和landscapright。和 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } - (NSUInteger)supportedInterfaceOrientations { 返回 UIInterfaceOrientationPortrait; } 它仍然旋转到横向
  • 你可以尝试使用“UIInterfaceOrientationMaskPortrait”(注意“mask”这个词)看看它是否能解决问题?即 -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
  • 而我把nslogs只调用了supportedInterfaceOrientations。未调用 preferredInterfaceOrientationForPresentation。
  • @Durgaprasad,请展示您的新代码并描述问题,而不是简单地“不工作”。
  • 谢谢。有效。关于ios 5 会调用这个方法还是会调用shouldAutorotateToInterface?
猜你喜欢
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多