Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Presenting a view controller in landscape right from a view controller supporting only portrait mode after rotating the iphone

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

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.

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;
}

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

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];

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?

like image 600
Tíbó Avatar asked Jan 28 '26 01:01

Tíbó


1 Answers

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

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];}

Edit: In 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
    }
}
like image 196
Tíbó Avatar answered Jan 30 '26 18:01

Tíbó



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!