1

I have this solution: How to lock orientation of one view controller to portrait mode only in Swift

It was working fine but now on iOS 13 it didn't work anymore.

I try this solution: Screen rotation glitch on iPadOS 13

But it doesn't fix it.

Can someone have a working solution for iPhone app in portrait but only one UIViewController in landscape?

Edit: I made a sample app, it is working fine with iOS 12.4.2 and iPhone 6. But my problem it isn't working with iOS 13.1.3 and iPhone X. => we.tl/t-I3Um0U43fV

Edit 2: Thanks to @donnywals, I success to keep my orientation as on iOS 12. I just need some help to clean the main screen during transition. The "TOP" label is 90px from top of superview. As you can see, the current portrait page is resized during the transition.

Anyone know how to avoid it?

enter image description here

Dam
  • 579
  • 6
  • 22

2 Answers2

1

You can lock a view controller's orientation by overriding its supportedInterfaceOrientations property:

class ViewController: UIViewController {

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscape
  }

  // rest of your code
}

Edit: Based on the sample project, you need to opt-out of the new presentation style to get the same behavior you had on iOS 13. Either set modalPresentationStyle = .fullScreen on the view controller that you're presenting in code or select the corresponding option in Interface Builder.

donnywals
  • 7,241
  • 1
  • 19
  • 27
  • That is the second link. It works perfectly on iOS 12 but not on iOS 13 :( I will edit my question with a sample app. – Dam Nov 08 '19 at 13:22
  • Thank you, I find it just now. It works, but for me Main screen have black part on top and bottom of the screen during transition. Do you know how to avoid it? – Dam Nov 08 '19 at 14:26
-1

This is a tested code and I have implemented the same and its working for me - Forcing a controller to landscape mode

In the method viewWillAppear add the following:

NSNumber *value=[NSNumber numberWithInt: UIDeviceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

Add the following methods to the view controller

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;}

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation{
return (UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight);}

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

- (BOOL)shouldAutorotate {
return true;}
Preetika
  • 702
  • 8
  • 21