iOS:在旋转iphone后,从仅支持纵向模式的视图控制器直接呈现横向视图控制器 [英] iOS: Presenting a view controller in landscape right from a view controller supporting only portrait mode after rotating the iphone

查看:116
本文介绍了iOS:在旋转iphone后,从仅支持纵向模式的视图控制器直接呈现横向视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序始终以纵向模式呈现(在Xcode项目的摘要中,仅支持纵向)。

I have one app always presenting in Portrait mode (in the summary of the Xcode project, only the portrait orientation is supported).

现在我想做什么当我正在使用应用程序时,从应用程序的任何视图控制器,如果我在横向右侧旋转设备,该应用程序在横向右侧呈现视图控制器(ARViewController.m),实际上旋转到横向右侧是触发器呈现ARViewController.m。但我所经历的是,因为第一个视图控制器只支持肖像,即使我将设备定位在横向右侧,我想从第一个视图控制器(ARViewController.m)呈现的视图控制器(ARViewController.m)也是纵向的,而不是在横向右边。

Now what I want to do is when I'm using the app, from any view controllers of the app, if I rotate the device in landscape right, the app presents a view controller (ARViewController.m) in landscape right, in fact the rotation to landscape right is the trigger to present ARViewController.m. But what I've experienced is, since the the first view controller only supports portrait and even if I orient the device in landscape right, the view controller (ARViewController.m) I want to present from the first one is in portrait too, not in landscape right.

即使我在第二个视图控制器(ARViewController.m)中写这个,它也不会自动旋转(这个视图控制器可以在每个方向呈现):

Even if I write this in the second view controller (ARViewController.m), it doesn't autorotate (this view controller can be presented in every orientations):

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ 
  return YES;
}

我必须在第二个视图控制器之后旋转iphone一次(ARViewController.m )它被提供为按顺序排列。

I have to rotate the iphone once after the second view controller (ARViewController.m) is presented to have all in order.

以下是我从第一个视图控制器调用第二个视图控制器(ARViewController.m)的方法:

And here is how I call this second view controller (ARViewController.m) from the first view controller:

ARViewController *arVC = [[ARViewController alloc] initWithNibName:@"ARViewController" bundle:nil];
[self presentModalViewController:arVC animated:YES];

我从ViewController.m调用它,这个被定义为<$ c AppDelegate.m中的$ c> rootViewController 。

I'm calling it from "ViewController.m", this one is defined as the rootViewController in the AppDelegate.m.

这是我第一次做这些事情,我一直在寻找解决方案,但仍然存在同样的问题。关于这个的任何提示?

This is the first time I'm doing such things, I've looked for solutions but still the same problem. Any tips on this?

推荐答案

我终于解决了这个问题,我想有其他选择,但这个工作正常:

I finally solved this problem, I suppose there are alternatives but this one works fine:

事实上,我只保留了纵向限制。然后当我向右或向左转动手机时,我会以模态方式调用我的AR​​ViewController,但在加载之前,我强制将此视图控制器设置为横向(在viewWillAppear中)进行适当的旋转,如下所示:

In fact I kept only Portrait in the orientation restrictions. Then when I turn the phone in landscape right or left, I call my ARViewController modally, but before loading it I force this view controller to landscape (in viewWillAppear) by making an appropriate rotation like here:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self transformView2ToLandscape];}

-(void) transformView2ToLandscape {

NSInteger rotationDirection;
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];

if(currentOrientation == UIDeviceOrientationLandscapeLeft){
    rotationDirection = 1;
}else {
    rotationDirection = -1;
}

CGAffineTransform transform = [arController.viewController.view transform];
transform = CGAffineTransformRotate(transform, degreesToRadians(rotationDirection * 90));
[arController.viewController.view setTransform: transform];}

编辑:在Swift 4中

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    transformViewToLansdcape()
}

func transformViewToLansdcape(){
    var rotationDir : Int
    if(UIDeviceOrientationIsLandscape(UIDevice.current.orientation)){
        rotationDir = 1
    }else{
        rotationDir = -1
    }
    var transform = self.view.transform
    //90 for landscapeLeft and 270 for landscapeRight
    transform = transform.rotated(by: (rotationDir*270).degreesToRadians)
    self.view.transform = transform
}

extension BinaryInteger {
    var degreesToRadians: CGFloat {
        return CGFloat(Int(self)) * .pi / 180
    }
}

这篇关于iOS:在旋转iphone后,从仅支持纵向模式的视图控制器直接呈现横向视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆