【问题标题】:iOS 6 Tab Bar App: shouldAutorotate not workingiOS 6 标签栏应用程序:shouldAutorotate 不起作用
【发布时间】:2013-02-15 14:32:53
【问题描述】:

我正在使用 iOS 6 和 Xcode 4.5 在 Storyboard 中开发一个带有标签栏和一些导航视图控制器的应用程序

通常应用应该支持所有界面方向,但我有两个只应该支持纵向模式的视图。

所以我在视图控制器中添加了以下代码:

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationPortrait;
}

在我在 iOS 6 上开发的另一个没有故事板和导航视图控制器的应用程序上它可以工作,但她不行! :/

我希望有人可以提供帮助,因为我发现了其他一些没有帮助的帖子...

来自德国的问候

劳伦兹

编辑:

我也试过 - 不起作用! :

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;

} 

【问题讨论】:

    标签: ios6 uitabbarcontroller uiinterfaceorientation


    【解决方案1】:

    据我所知,出现此问题是因为 UITabBarController 和 UINavigationController 正在返回它们自己的默认值 -(BOOL)shouldAutorotate 和 -(NSUInteger)supportedInterfaceOrientations。

    一种解决方案是通过类别(或只是子类)扩展这两个类,以便从您自己在视图控制器中的这些方法的实现中返回适当的值。这对我有用(你可以把它放到你的 App Delegate 中):

    @implementation UITabBarController(AutorotationFromSelectedView)
    
    - (BOOL)shouldAutorotate {
        if (self.selectedViewController) {
            return [self.selectedViewController shouldAutorotate];
        } else {
            return YES;
        }
    }
    
    - (NSUInteger)supportedInterfaceOrientations {
        if (self.selectedViewController) {
            return [self.selectedViewController supportedInterfaceOrientations];
        } else {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    }
    
    @end
    
    @implementation UINavigationController(AutorotationFromVisibleView)
    
    - (BOOL)shouldAutorotate {
        if (self.visibleViewController) {
            return [self.visibleViewController shouldAutorotate];
        } else {
            return YES;
        }
    }
    
    - (NSUInteger)supportedInterfaceOrientations {
        if (self.visibleViewController) {
            return [self.visibleViewController supportedInterfaceOrientations];
        } else {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    }
    @end
    

    默认情况下,您的所有视图控制器将继续自动旋转。在应该只支持纵向模式的两个 View Controller 中,实现以下内容:

    -(BOOL)shouldAutorotate {
        return NO;
    }
    
    -(NSUInteger)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskPortrait;
    }
    

    【讨论】:

    • 我在 appdelegate.m 和我的 viewcontroller.m 中实现了您的代码。它现在正在锁定 viewcontroller.m 的横向模式,但是当我从以前的视图以横向模式转到 viewcontroller.m 时,它会以横向模式显示屏幕,这次它不会更改为 potrait 模式。
    • 这是正确答案。我在这方面遇到了很多麻烦,并对 UITab 和 UINav 进行分类修复了它。您应该将此答案标记为正确。
    • 我已扩展此代码以支持 UISplitViewController:gist.github.com/cameroncooke/a9244bc4d677f50940f5
    【解决方案2】:

    乔纳森的出色回答。

    我稍微修改了他的代码以在单个 sn-p 中处理导航控制器。

    - (BOOL)shouldAutorotate {
        if (self.selectedViewController) {
            if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
                return [[[(UINavigationController*)self.selectedViewController viewControllers] lastObject] shouldAutorotate];
            }
            return [self.selectedViewController shouldAutorotate];
        } else {
            return YES;
        }
    }
    
    - (NSUInteger)supportedInterfaceOrientations {
        if (self.selectedViewController) {
            if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
                return [[[(UINavigationController*)self.selectedViewController viewControllers] lastObject] supportedInterfaceOrientations];
            }
            return [self.selectedViewController supportedInterfaceOrientations];
        } else {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-17
      • 2012-09-25
      • 1970-01-01
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      相关资源
      最近更新 更多