【问题标题】:iOS 6: how to force change orientation when pushing view controller into the navigation controller stack [duplicate]iOS 6:将视图控制器推入导航控制器堆栈时如何强制更改方向[重复]
【发布时间】:2013-04-28 12:58:17
【问题描述】:

我认为这个问题现在应该已经被问了一百万次了,但我仍然找不到答案。

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

UINavigationController:支持所有可能的方向 UIViewController 1:只支持纵向 UIViewController 2:仅支持横向

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

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

谢谢!

【问题讨论】:

  • 试试这个它会为你工作:stackoverflow.com/questions/12520030/…
  • 您找不到答案,因为它无法完成。尽管有以下所有答案,但 iOS 6 根本不支持您要求做的事情。iOS 6 允许响应用户旋转设备,但不允许强制旋转。在 iOS 6 中强制 旋转的唯一方法是使用呈现的视图控制器,而不是推送到导航控制器堆栈的视图控制器。
  • 但是,您可以构建一个呈现的视图控制器,使其看起来就好像它是同一导航界面的一部分。并且由于呈现的视图控制器可以在呈现和关闭时强制旋转,这有效地解决了问题。例如,请参阅我的答案:stackoverflow.com/a/4103137/341994
  • 我制作了一部电影来展示我的意思:youtu.be/O76d6FhPXlE 正如你所看到的,它有点看起来就像我正在将第二个视图推送到导航控制器上并且强制旋转。但实际上有两个导航控制器;第二个出现(模态),所以我可以在它出现和被关闭时强制旋转。
  • 谢谢@matt。这完全解释了为什么我到目前为止所尝试的一切都不起作用。特别感谢您为详细解释甚至制作短片所做的努力。超级有帮助!!由于我无法将您的评论标记为已接受的答案,因此我只是对您的评论投了赞成票。

标签: ios xcode ios6 uinavigationcontroller orientation


【解决方案1】:

我也发现了同样的问题。我发现 shouldAutorotate 函数不是每次都调用所以我改变方向编程

先导入这个

#import <objc/message.h>

然后

-(void)viewDidAppear:(BOOL)animated
{

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


        }
    }

}

希望对你有所帮助。

【讨论】:

  • setOrientation 是一种未记录的方法,使用它可能会导致您的应用从 AppStore 中退出。使用风险自负。
  • 不,这不是真的。我也在我的应用程序中使用这种方法,这在应用程序商店中是实时的。
  • Apple 曾经有一个公共的读写属性,用于提供方向。他们将其更改为只读。以上是一个危险的技巧,让您可以访问他们的私人二传手。这很可能导致您的应用被拒绝。在你的情况下它没有,但使用它需要你自担风险。
  • 终于!谢谢!简洁明了!
  • 这会导致界面根据需要旋转,但不要忘记在之后立即切换回来,否则设备会通过在第二行添加此权利来认为它的方向错误:objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), _yourRealInterfaceOrientation );
【解决方案2】:

添加新的 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];
  }

添加新类后,转到您的 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;
  }

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

这样做的原因:-

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

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

希望对你有帮助!

【讨论】:

  • 将它作为一个类别附加到 AppDelegate 更简洁、更容易,然后为此创建一个新类,但它可以双向工作。
  • +1 这个不错的答案。这是一种适合轮换的方式。
  • 太棒了!工作和好的解决方案
  • 嗯..没有。它无法正常工作。不知道我昨天是怎么检查的,但是在单击后退按钮时,父视图控制器会获得儿子的方向
【解决方案3】:

使应用只支持纵向模式,并在 UIViewController 2 的 initWithNibName 中添加以下行

self.view.transform = CGAffineTransformMakeRotation(M_PI/2);

【讨论】:

  • 这不是改变方向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 2014-08-21
  • 1970-01-01
  • 2020-07-14
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
相关资源
最近更新 更多