iOS 6:将视图控制器推入导航控制器堆栈时如何强制更改方向

ymo*_*tov 4 xcode orientation uinavigationcontroller ios ios6

我觉得这个问题到现在应该被问过一百万次,但我仍然无法找到答案.

这是我的层次结构:UINavigationController - > UIViewController 1 - >(推送) - > UIViewController 2

UINavigationController:支持所有可能的方向UIViewController 1:仅支持portrait UIViewController 2:仅支持landscape

如何仅将UIViewController 1锁定为纵向,同时仅将UIViewController 2锁定为横向?它甚至可能吗?到目前为止,我看到的是UIViewController 2总是采用UIViewController 1的方向.

请注意,这仅适用于iOS 6.

谢谢!

Sum*_*dra 13

我也发现了同样的问题.我发现应该每次调用功能都没有调用,所以我改变了方向程序

先导入这个

#import <objc/message.h>
Run Code Online (Sandbox Code Playgroud)

然后

-(void)viewDidAppear:(BOOL)animated
{

     if(UIDeviceOrientationIsPortrait(self.interfaceOrientation)){
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
        {
            objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft );


        }
    }

}
Run Code Online (Sandbox Code Playgroud)

希望这能帮助你.


Gau*_*ogi 9

添加新的Objective-C类(UINavigationController的子类)并将以下代码添加到.m文件中

-(NSUInteger)supportedInterfaceOrientations
 {
     NSLog(@"supportedInterfaceOrientations = %d ", [self.topViewController         supportedInterfaceOrientations]);

     return [self.topViewController supportedInterfaceOrientations];
 }

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

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
    // You do not need this method if you are not supporting earlier iOS Versions

    return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
  }
Run Code Online (Sandbox Code Playgroud)

添加新类后,转到ViewController类并进行以下更改

- (BOOL)shouldAutorotate  // iOS 6 autorotation fix
  {
    return YES;
  }
- (NSUInteger)supportedInterfaceOrientations // iOS 6 autorotation fix
  {
      return UIInterfaceOrientationMaskAll;
  }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix
  {
      return UIInterfaceOrientationPortrait;
  }
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  {
      return YES;
  }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在shouldAutorotate中,shouldAutorotateToInterfaceOrientation:如果你想让ViewController支持多个方向,则返回YES,否则返回NO,同样在houldAutorotateToInterfaceOrientation中:方法传递你想要的特定ViewController的Orintation,对所有视图控制器重复相同的操作.

这样做的原因: -

1:虽然您可以将preferredInterfaceOrientationForPresentation:任何viewController更改为特定方向,但由于您使用的是UINavigationController,因此您还需要覆盖UINavigationController的supportedInterfaceOrientations

2:为了覆盖UINavigationController的supportedInterfaceOrientations,我们将子类化为UINavigationController并修改了与UINavigation Orientation相关的方法.

希望它能帮到你!

  • hmm..no.它工作不正常.不知道我昨天如何检查它,但点击后退按钮,父视图控制器获得儿子的方向 (3认同)