3

I have an app that is portait mode only. I only have one View Controller that I need to force into landscape mode (A customer signature view) so there is more room for the customer to sign.

How can I force one ViewController to appear in landscape mode. Leaving the rest in portrait?

I have tried setting the app's General Deployment info to Portrait, Landscape Left

enter image description here

And then using this code on the Signature View

- (BOOL)shouldAutorotate {
    return YES;
}

and returning NO on all other view controllers. But that didn't seem to work.

I also included

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

Is this possible?

user-44651
  • 3,924
  • 6
  • 41
  • 87

3 Answers3

1

Try this code in the Signature View Controller

 - (void)viewDidLoad
{
    [super viewDidLoad];
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
}
  • Deprecated :( https://stackoverflow.com/questions/75594819/crash-when-locking-screen-orientation-in-swiftui-using-uidevice-current-setvalue – John Gorenfeld Mar 15 '23 at 15:33
0

You're just missing the following code below, if your UIViewController is a rootcontroller of a navigationcontroller you need to put the code in the navigationcontroller. Hope this helps, good luck.

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}
JingJingTao
  • 1,760
  • 17
  • 28
  • That semi worked. Autolayout seems to be thrown out the window as all the elements are incorrectly laid out. Additionally if I leave the landscaped view, all the other views are now stuck in the landscape.. – user-44651 Jul 21 '16 at 13:11
  • Could you post an image of what your storyboard looks like with the constraints you use and what it looks like incorrectly laid out in landscape. So I used this flow and it worked, navigationcontrollerA -> viewcontrollerA -> segue -> navigationcontrollerB -> viewcontrollerB, in navB I set 'supportedInterfaceOrientations' and 'shouldAutorotate', vcA appears in portrait, vcB appears in landscape, I have to use `self.navigationController dismissViewControllerAnimated` to pop from vcB to vcA. – JingJingTao Jul 21 '16 at 14:51
0

Following is the the way its working for me:-

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

and 

- (BOOL)shouldAutorotate {
    return NO;
}

Another way is to define orientation in prepareForSegue and fixed orientation of the required VC and set shouldAutorotate to NO in that VC.

Let me know if mentioned code doesn't work for you.

Meet
  • 1,196
  • 12
  • 26