25

我有一个只能在 中工作的应用程序Portrait Mode,但是有一个可以显示视频的 singleView,所以我希望该视图也可以在 中工作landscape mode,但是在 iOS 6 中我不知道该怎么做,现在我有了这个:

在 AppDelegate.mi 中有:

self.window.rootViewController = myTabBar;

然后在项目摘要中:

在此处输入图像描述

我发现在 iOS 6 中要检测视图旋转我必须这样做:

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}

// Tell the system It should autorotate
- (BOOL) shouldAutorotate {
return YES;
}

所以我只在我想在横向中使用的代码中插入上面的代码UIViewController,但是不起作用,有人知道我该怎么做吗?我只想在显示视频时自动旋转。

4

3 回答 3

49

首先,您的目标设置应如下所示: 支持的接口方向

在 UITabBarController 中:

-(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
{
    if (self.selectedViewController) 
        return [self.selectedViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

在你的 ViewController 里面:

a) 如果您不想旋转:

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

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

b)如果你想旋转到横向:

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

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

编辑:

其他解决方案是在 AppDelegate 中实现此方法:

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

    if (self.window.rootViewController) {
        UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presented supportedInterfaceOrientations];
    }
    return orientations; 
}
于 2012-09-23T10:57:38.573 回答
3

我会写评论,但我不能,所以我将其发布为答案。

这是我的场景:

我的应用程序仅支持在某些视图上更改方向,我不知道如何仅针对我想要的视图进行更改,然后我遇到了这个问题并看到了 mientus 的回答(谢谢)然后我继续做了什么他建议哪个是子类 UITabBarController 并覆盖这些方法:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{

    NSLog(@"AUTO ROTATE IN CUSTOM TAB BAR");
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}


-(NSUInteger)supportedInterfaceOrientations{

    NSLog(@"supportedInterfaceOrientations IN CUSTOM TAB BAR");

    if (self.selectedViewController)
        return [self.selectedViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate{

    NSLog(@"shouldAutorotate IN CUSTOM TAB BAR");
    return [self.selectedViewController shouldAutorotate];
}

然后在每个视图控制器中,我将有方法来指示我是否想要旋转。UITabBarController 中的方法被调用,但不是我的视图控制器中的方法,因此旋转仍在我不想发生的地方发生。然后我继承 UINavigationController 并覆盖相同的方法,仅在 supportedInterfaceOrientation 上进行此更改,如下所示:

- (NSUInteger)supportedInterfaceOrientations{

NSLog(@"supportedInterfaceOrientations IN CUSTOM NAV BAR CALLING CURRENT VIEW CONTROLLER");
UIViewController* presented = [[self viewControllers] lastObject];
return [presented supportedInterfaceOrientations];

}

这基本上是做什么的,它获取当前视图控制器,然后询问支持的方向,瞧,我的视图控制器中的方法被调用,我可以在我想要的地方处理方向。

于 2013-02-06T20:29:10.173 回答
0

您要旋转的视图是仅纵向视图的子视图吗?通常视图旋转行为继承自 rootviewcontroller。然后,如果您在 rootviewcontroller 中的 shouldAutorotate 中返回 NO,您将停止每个下视图中的旋转。

我建议以这种方式拆分您的架构:

rootViewController -> supportedInterfaceOrientations = Portrait & shouldAutorotate = YES NORotationViewControllers -> supportedInterfaceOrientations = Portrait & shouldAutorotate = YES rotationViewControllers -> supportedInterfaceOrientations = All & shouldAutorotate = YES

如果您还没有阅读此内容,请查看: Supporting Multiple Interface Orientations

于 2012-09-23T10:48:35.900 回答