How to set device (UI) orientation programmaticall

2019-01-07 08:55发布

Would like everything on the screen (UI) to be able to rotate from landscape left to right or vica versa.

How do I go about doing this? Is this private?

I know that

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {}

lets you say which orientations the UI can be, but is there a way to force only one at a time?

Cheers

10条回答
叛逆
2楼-- · 2019-01-07 09:29

As suggested on a related thread shouldAutorotateToInterfaceOrientation is deprecated. Newer apps should leverage the following methods as described in Apple's UIViewController Class Reference until they themselves become deprecated:

  • shouldAutorotate,
  • preferredInterfaceOrientationForPresentation; and
  • attemptRotationToDeviceOrientation.

From what I understand it's best to use different View Controllers for views which need to lock into (i.e. have a preferred) orientation mode which differs from the last.

查看更多
beautiful°
3楼-- · 2019-01-07 09:33

Also this simple way works:

[[UIApplication sharedApplication] setStatusBarHidden:YES];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

and back:

[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
查看更多
对你真心纯属浪费
4楼-- · 2019-01-07 09:33

Even though this is not an idle solution, works well for me.

- (void)viewDidLoad
{
   self.view.transform = CGAffineTransformIdentity;
   self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0)); 
   self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
查看更多
Melony?
5楼-- · 2019-01-07 09:41

This was addressed by: Guntis Treulands https://stackoverflow.com/a/10146270/894671

//-----------------------------------
// FORCE PORTRAIT CODE
//-----------------------------------
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];
//present/dismiss viewcontroller in order to activate rotating.
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
查看更多
登录 后发表回答