ios-视图控制器不以当前方向加载

时间:2014-10-07 10:44:44

标签: ios objective-c

我正在使用UINavigationController的子类来管理我的应用程序的轮换。

@implementation rotatingNavigationController


- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

在我的第一个视图中,我只允许纵向显示:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

我使用故事板中创建的push segue推送到新的视图控制器。新的视图控制器支持所有接口方向:

- (BOOL)shouldAutorotate
{

    return YES;
}

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAll;
}

新的视图控制器始终以纵向方式加载,无论设备的方向如何。如果我将设备物理转换为纵向模式并在视图加载后返回横向,它将正确旋转。我怎样才能纠正这个问题,我需要视图加载视图加载时设备所处的方向?

1 个答案:

答案 0 :(得分:0)

当您推动新控制器时。然后检查状态栏的界面方向:

 -(BOOL)shouldAutorotate
{
   if(isPushingViewVC)
      return NO
   return YES;
}

并且

- (NSUInteger)supportedInterfaceOrientations
{
    if(isPushingViewVC)
    {
        switch ([UIApplication sharedApplication].statusBarOrientation)
        {
            case UIInterfaceOrientationLandscapeLeft:
                return UIInterfaceOrientationMaskLandscapeLeft;
                break;
            case UIInterfaceOrientationPortrait:
                return UIInterfaceOrientationMaskPortrait;
                break;
            case UIInterfaceOrientationPortraitUpsideDown:
                return UIInterfaceOrientationMaskPortraitUpsideDown;
                break;
            default:
                return UIInterfaceOrientationMaskLandscape;
                break;
        }
    }
     return UIInterfaceOrientationMaskAll;
}