skip to Main Content

In my app all screens have portrait orientation. How to allow only single UIViewController be in two orientation mode: portrait and upside down? When user rotates an iPhone, the UIViewController should rotates too.

2

Answers


  1. If you have any function that continuously called then you can check the device orientation inside this function as-

    if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown {
        // set your view constraints
    }
    
    if UIDevice.current.orientation == UIDeviceOrientation.portrait {
        // set your view constraints 
    }
    

    Otherwise you should create a monitoring function and check device orientation inside the function.

    Login or Signup to reply.
  2. First of all, In your application all screens except one screen are in portrait orientation. So You cannot set orientation of your application as portrait. So Set Device Orientation as portrait and LandScape both.

    Put following code on which screen you need landscape orientation in ViewDidLoad

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

    and

    override var shouldAutorotate: Bool {
    return true
    }
    

    Put following code on all screens where you need only portrait orientation in viewDidLoad

    let value = UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search