0

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 2

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")
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. But I don't neet landscape mode, only portrait and upside down in one UIViewController
0

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.

3 Comments

I need such logic: when user takes an iPhone in portrait orientation, UIViewController looks in portrait orientation, if user takes in upside down orientation, the same UIViewController looks in upside down orientation
Yes, I got it. When you get the upsideDown orientation you should change your view constraints. I mean new design for your view.
And at first, set your project device orientation only portrait. After this, your view will not change for landscape mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.