【问题标题】:iphone: Forcefully change orientation from portrait to landscape on navigationiphone:在导航时强制将方向从纵向更改为横向
【发布时间】:2012-07-21 14:06:25
【问题描述】:

有什么方法可以在导航时强制将应用方向从纵向更改为横向?

我有一个要求,在将控制器从 A 推到 B 时。B 应该是横向的,但我的 A 控制器是纵向的。

【问题讨论】:

  • 尝试在每个类中使用不同的-shouldAutorotateToInterfaceOrientation: 方法。这只是一个想法。
  • 您在UIInterfaceOrientationPortrait 中有应用程序,您想更改单视图的界面方向?

标签: iphone objective-c uinavigationcontroller orientation landscape


【解决方案1】:

在你的 ViewController-B 中,在 viewDidLoad 中添加这行代码:

  [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

  float   angle = M_PI/2;  //rotate 180°, or 1 π radians
  self.view.layer.transform = CATransform3DMakeRotation(angle, 0, 0.0, 1.0);

如果您使用导航控制器从 A 移动到 B,则此代码可以正常工作。

我用过这个。

希望这会对你有所帮助。

享受编码:)

【讨论】:

    【解决方案2】:

    - (void)viewWillAppear:(BOOL)animated 说:

        //this is to force the application to re-evaluate device orientation
        //comment the last lines and see
        //cannot use [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft];
        //as "setOrientation" is a private method, and we risk to be kicked out by Apple
        UIViewController *c = [[[UIViewController alloc]init] autorelease];
        [self presentModalViewController:c animated:NO];
        [self dismissModalViewControllerAnimated:NO];
    

    ...但首先,在您的 xib 中,将视图的方向设置为所需的方向

    【讨论】:

      【解决方案3】:

      根据我在上一个尝试这样做的应用程序的经验,我发现堆栈溢出的答案告诉不要强行更改基于导航的应用程序中的方向。

      (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
      {
       if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
              return NO;
      else
          return YES;
      }
      

      此功能可让您以所需的方向使用视图控制器,在这种情况下,我将其设置为横向,并且不允许在纵向模式下旋转。

      您将面临 B 视图控制器首先在 A 视图控制器上的方向加载的问题。所以我将应用程序的完整方向更改为横向。 希望这会有所帮助。

      【讨论】:

      • 嗯,当我尝试这个时,它是在 iOS 5 中,谢谢你的信息:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多