【问题标题】:Autorotate a single UIViewController in iOS 6 with UITabBar使用 UITabBar 在 iOS 6 中自动旋转单个 UIViewController
【发布时间】:2012-09-15 02:22:48
【问题描述】:

我有一个只能在 Portrait Mode 中使用的应用程序,但是有一个可以显示视频的单视图,所以我希望该视图也可以在 landscape mode 中使用,但是在 iOS 6 中我无法弄清楚我是如何做到的可以做到,现在我有了这个:

在 AppDelegate.m 我有:

self.window.rootViewController = myTabBar;

然后在项目摘要中:

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

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}

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

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

【问题讨论】:

    标签: iphone uiviewcontroller ios6 landscape-portrait


    【解决方案1】:

    首先,您的目标设置应如下所示:

    在 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; 
    }
    

    【讨论】:

    • 感谢您的回答,但如果我以这种方式创建 UITabBar,我无法理解如何以及在何处编写 UITabBarController 的代码: UITabBarController *myTabBar = [UITabBarController alloc] init];然后我为标签栏设置视图控制器,我在 App Delegate 的 didfinish 方法中完成这一切......
    • UITabBar 只是一些控件,你需要告诉我你是在哪个 contrainer 上添加的,看 Beppe 的文章。
    • 在 AppDelegate 中的 didFinishLaunchingWithOptions 中确定,我会这样做:self.window.rootViewController = myTabBar;所以我必须在应用程序委托中执行该代码?
    • myTabBar 是哪种类型? UITabBarController ?
    【解决方案2】:

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

    这是我的场景:

    我的应用程序仅支持在某些视图上更改方向,我不知道如何仅针对我想要的视图进行更改,然后我遇到了这个问题并看到了 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];
    

    }

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

    【讨论】:

      【解决方案3】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多