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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16370488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:19:31  来源:igfitidea点击:

iOS 6: how to force change orientation when pushing view controller into the navigation controller stack

iosxcodeios6uinavigationcontrollerorientation

提问by ymotov

I think this question should've been asked a million times by now, but I still can't find an answer to this.

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

Here's my hierarchy: UINavigationController -> UIViewController 1 ->(push)-> UIViewController 2

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

UINavigationController: supports all possible orientation UIViewController 1: supports only portrait UIViewController 2: supports only landscape

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

How can I lock UIViewController 1 into portrait only and at the same time lock UIViewController 2 into landscape only? Is it even possible? So far what I see is that UIViewController 2 always takes the orientation of UIViewController 1.

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

Note, that this is for iOS 6 only.

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

Thanks!

谢谢!

回答by Sumit Mundra

I am also find the same problem.i have found that shouldAutorotate function not call every time so i change orientation programmatic

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

first import this

首先导入这个

#import <objc/message.h>

then

然后

-(void)viewDidAppear:(BOOL)animated
{

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


        }
    }

}

hope this help you.

希望这对你有帮助。

回答by Gaurav Rastogi

Add new Objective-C class (subclass of UINavigationController) and add the following code to the .m files

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

After you added the new classes go to your ViewController classes and make the following changes

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

enter image description here

在此处输入图片说明

In the shouldAutorotate , shouldAutorotateToInterfaceOrientation: return YES if you want the ViewController to be supporting Multiple orientation else return NO , also in houldAutorotateToInterfaceOrientation: method pass the Orintation you want for that specific ViewController , Repeat the same for all the view controllers .

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

Reason of doing this:-

这样做的原因:-

1:Although you can change the preferredInterfaceOrientationForPresentation: of any viewController to a specific orientation but since you are using the UINavigationController you also need to override the supportedInterfaceOrientations for your UINavigationController

1:尽管您可以将任何视图控制器的 preferredInterfaceOrientationForPresentation: 更改为特定方向,但由于您使用的是 UINavigationController,因此您还需要覆盖 UINavigationController 的 supportedInterfaceOrientations

2:In order the override the supportedInterfaceOrientations for UINavigationController we have subclassed UINavigationController and modified the method related to the UINavigation Orientation.

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

Hope it will help you !

希望它会帮助你!

回答by William T. Wallace

Make the app support only portrait mode and add the following line to the initWithNibName of UIViewController 2

使应用程序仅支持纵向模式并将以下行添加到 UIViewController 2 的 initWithNibName

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