How to programmatically configure device orientation (UI)?

I wish that everything on the screen (UI) could rotate from the landscape from left to right or vice versa.

How should I do it? Is this personal?

I know that

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

allows you to tell what orientation the user interface may be, but is there a way to force only one at a time?

Greetings

+23
ios objective-c iphone orientation
Dec 02 2018-10-12T00:
source share
10 answers

This method is called to determine if your interface should automatically rotate to a given rotation (i.e. letting UIKit do the hard work, rather than doing it manually).

So, if you want your application to work only in landscape, you must implement the body of this method with

return UIInterfaceOrientationIsLandscape(interfaceOrientation);

If you want your user interface to automatically rotate in all directions, you could simply return YES;

Is that what you asked?

+16
Dec 02 2018-10-12T00:
source share

On iOS, [[UIDevice currentDevice] setDeviceOrientation:UIDeviceOrientationSomeOrientation] method is missing. But we can rotate the view with the status bar, for this:

 - (void)showAlbum { // check current orientation if ([[UIApplication sharedApplication] statusBarOrientation] != UIInterfaceOrientationLandscapeLeft) { // no, the orientation is wrong, we must rotate the UI self.navigationController.view.userInteractionEnabled = NO; [UIView beginAnimations:@"newAlbum" context:NULL]; [UIView setAnimationDelegate:self]; // when rotation is done, we can add new views, because UI orientation is OK [UIView setAnimationDidStopSelector:@selector(addAlbum)]; // setup status bar [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO]; // rotate main view, in this sample the view of navigation controller is the root view in main window [self.navigationController.view setTransform: CGAffineTransformMakeRotation(M_PI / 2)]; // set size of view [self.navigationController.view setFrame:CGRectMake(0, 0, 748, 1024)]; [UIView commitAnimations]; } else { [self addAlbum]; } } 
+24
Jan 31 '11 at 11:52
source share

I had success with this:

  if (self.interfaceOrientation != UIInterfaceOrientationPortrait) { // http://stackoverflow.com/questions/181780/is-there-a-documented-way-to-set-the-iphone-orientation // http://openradar.appspot.com/radar?id=697 // [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; // Using the following code to get around apple static analysis... [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait]; } 
+18
Jun 20 2018-12-12T00:
source share

If you present a modal view controller that implements -shouldAutorotateToInterfaceOrientation: only to support one orientation, the entire interface will be automatically forced into it. I don’t think there is a way to programmatically change the orientation otherwise.

+5
Dec 02 '10 at 2:26
source share

Also this simple way works:

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

and back:

 [[UIApplication sharedApplication] setStatusBarHidden:NO]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]; 
+3
Jun 21 '12 at 13:13
source share

In Swift, change the orientation to the portrait:

 UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation") 

See: stack overflow.squite

+3
Mar 18 '15 at 15:38
source share

It was reviewed by: Guntis Treulands https://stackoverflow.com/a/312877/

 //----------------------------------- // 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]; 
+2
Feb 15 '13 at 0:54
source share
 if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft ); } 
0
Aug 27 '14 at 11:03
source share

Although this is not an idle solution, it 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); } 
0
Apr 07 '15 at 6:42
source share

As suggested in the appropriate thread shouldAutorotateToInterfaceOrientation deprecated. New applications should use the following methods described in the Apple UIViewController class reference until they themselves become obsolete:

  • shouldAutorotate ,
  • preferredInterfaceOrientationForPresentation ; and
  • attemptRotationToDeviceOrientation .

From what I understand, it’s best to use different View Controllers for views that need to block (i.e. have a preferred) orientation mode that is different from the last.

0
Jul 24 '15 at 20:13
source share



All Articles