Search code examples
iosobjective-cuiviewcontrollerlandscapedevice-orientation

Force rotate UIViewController in Portrait status


In this environment (Fix the support orientation is Portrait in Project configuration )

enter image description here

Can you show a specific UIViewController in Landscape?


Solution

  • The short answer is that you cannot because according to the Apple docs here

    The system intersects the view controller's supported orientations with the app's supported orientations (as determined by the Info.plist file or the app delegate's application(_:supportedInterfaceOrientationsFor:) method) and the device's supported orientations to determine whether to rotate.

    To implement what you need you should set the allowed orientations to landscape too, then implement the following in your view controllers to allow either the portrait or landscape orientations (or both):

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait//or UIInterfaceOrientationMask.landscape
      }
    

    Then to force a specific orientation you can set the orientation to the UIDevice:

    UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")