Set UIInterfaceOrientation at Run Time

I have 5 views in the storyboard. I want each view orientation to be different, for example, one view will be UIInterfaceOrientationLandscapeRight another view will be UIInterfaceOrientationLandscapeLeft or UIInterfaceOrientationPortrait or UIInterfaceOrientationPortraitUpsideDown . So, how to set this view orientation in different ways.

I used [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; but it shows the warning "UIDevice may not respond to setOrientation". According to many stackoverflow authors, I use

 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { //return UIInterfaceOrientationLandscapeLeft; return (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown); } 

this code above, but it does not work in my project. So what is the correct screen orientation procedure. Thanks in advance.

+4
source share
3 answers

try it

 [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES]; 

You cannot set UIDeviceOrientation because it is a physical parameter. If you hold the device in your hand in the portrait - how do you programmatically install it in the album?). UIDeviceOrientation means - The orientation of the device, not the Interface. This answer describes the differences between the orientations: fooobar.com/questions/15015 / ...

+2
source

You should put -(BOOL)shouldAutorotateToInterfaceOrientation: in each viewController and check that the orientation is correct. If you click on a viewController that only supports, for example. upsideDown, it will be presented upside down.

Note:

  • If you are in a tabbed environment, the behavior is different. Please ask if this is so.
  • If you are talking about rootViewController, your application should be configured corresponding to the first viewController (Info.plist)
0
source

The orientation property in the UIDevice is read-only .

You can use [UIViewController attemptRotationToDeviceOrientation]; to try to rotate all view controllers in the current device orientation. You cannot force the rotation.

0
source

Source: https://habr.com/ru/post/1413524/


All Articles