How to set device (UI) orientation programmatically?
Asked Answered
P

10

23

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

Psychopharmacology answered 2/12, 2010 at 1:56 Comment(3)
shouldAutorotateToInterfaceOrientation is deprecated in iOS 6.0. See here.Pentimento
FORCE PORTRAIT CODE, for my app its work https://mcmap.net/q/282583/-how-to-change-the-device-orientation-programmatically-in-ios-6-duplicateSentient
Possible duplicate of How to programmatically determine iPhone interface orientation?Jenisejenkel
L
16

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

So if you wanted your app to only work in landscape you'd implement the body of that method with:

return UIInterfaceOrientationIsLandscape(interfaceOrientation);

If you wanted your UI to auto rotate to all orientations you could just return YES;

Is that what you were asking?

L answered 2/12, 2010 at 2:6 Comment(6)
no, rather setting the UI orientation to different orientations by code at different times in the program. (so, for some context, it can mirror another device)Psychopharmacology
Oh OK, well in that case I would probably return YES for the orientations that your VC supports in that delegate method, and then try to force the orientation using: [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; I don't know if I've ever personally tried forcing it that way, but it might work...L
Not sure if this is your problem, but change your shouldAutorotateToInterfaceOrientation method to: return UIInterfaceOrientationIsLandscape(interfaceOrientation);L
And then maybe do what Justin Spahr-Summers says and modally present the view controller. That way you might not have to force the orientation using setOrientation. Doing crazy orientations can be a bitch...L
@half_brick : so I did what you told and it works nicely. My nib file is in landscape mode. and the specific view is always in landscape. however, if i rotate the hardware in landscape (the other way), the view controller doesn't rotate ... any help ?Lonna
You guys are talking about a specific nib... but what about device orientation? Does device orientation just match whatever the nib's orientation is? I've got a game where the glView is rendering such that it appears in landscape but the device (or something else?) is preserving a portrait orientation. I'm having trouble placing your answer in that scope...Enthuse
S
24

In iOS [[UIDevice currentDevice] setDeviceOrientation:UIDeviceOrientationSomeOrientation] method is absent. But we can rotate a view with 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];
    }

}
Sulph answered 31/1, 2011 at 11:52 Comment(4)
Thanks! All I needed was the UIView.transform property since the view I'm working on only needs to be in landscape orientation.Pickens
What about CoreMotion orientation? Does it change with status bar orientation?Uptodate
No,it doesnt. CoreMotion sends device-motion events this code cannt change device orientation it has changed just application screen orientation.Sulph
I don't know how you can use an UIDeviceOrientation for setStatusBarOrientation. According to the class reference, you need to use an UIInterfaceOrientation. But perhaps you can explain your reasoning?Pentimento
F
18

I've had success with this:

 if (self.interfaceOrientation != UIInterfaceOrientationPortrait) {
    // https://mcmap.net/q/162625/-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's static analysis...
    [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
}
Fall answered 20/6, 2012 at 3:56 Comment(4)
First thank you Chis!! But why did Apple make this method (setOrientation:) not avaliable for us? It's so easy and simple. But is it Apple proove also? ThxCherice
By using this method any chance of rejection in apple approval process?Wirehaired
The above code will not compile under ARC. It does not appreciate the cast from NSInteger to id. You can, however, use NSInvocation instead. That said, it didn't work for me under iOS 6.Nor
You can overcome the ARC issue with using: [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)]; but it would not work under iOS6 anyway.Ermin
L
16

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

So if you wanted your app to only work in landscape you'd implement the body of that method with:

return UIInterfaceOrientationIsLandscape(interfaceOrientation);

If you wanted your UI to auto rotate to all orientations you could just return YES;

Is that what you were asking?

L answered 2/12, 2010 at 2:6 Comment(6)
no, rather setting the UI orientation to different orientations by code at different times in the program. (so, for some context, it can mirror another device)Psychopharmacology
Oh OK, well in that case I would probably return YES for the orientations that your VC supports in that delegate method, and then try to force the orientation using: [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; I don't know if I've ever personally tried forcing it that way, but it might work...L
Not sure if this is your problem, but change your shouldAutorotateToInterfaceOrientation method to: return UIInterfaceOrientationIsLandscape(interfaceOrientation);L
And then maybe do what Justin Spahr-Summers says and modally present the view controller. That way you might not have to force the orientation using setOrientation. Doing crazy orientations can be a bitch...L
@half_brick : so I did what you told and it works nicely. My nib file is in landscape mode. and the specific view is always in landscape. however, if i rotate the hardware in landscape (the other way), the view controller doesn't rotate ... any help ?Lonna
You guys are talking about a specific nib... but what about device orientation? Does device orientation just match whatever the nib's orientation is? I've got a game where the glView is rendering such that it appears in landscape but the device (or something else?) is preserving a portrait orientation. I'm having trouble placing your answer in that scope...Enthuse
P
5

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

Phonon answered 2/12, 2010 at 2:26 Comment(0)
A
3

Also this simple way works:

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

and back:

[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
Ayr answered 21/6, 2012 at 13:13 Comment(1)
This helped me fixing IPAD landscape launch issue: no touch event received at all. Calling setStatusBarHidden magically solves the problem.Despiteful
V
3

In Swift to change the orientation to portrait:

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

See: https://stackoverflow.com/a/24259601

Voice answered 18/3, 2015 at 15:38 Comment(1)
Thank you, I needed this answer!Issuance
D
2

This was addressed by: Guntis Treulands https://mcmap.net/q/282584/-iphone-landscape-faq-and-solutions

//-----------------------------------
// 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];
Displacement answered 15/2, 2013 at 0:54 Comment(0)
G
0
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        objc_msgSend([UIDevice currentDevice], @selector(setOrientation:),    UIInterfaceOrientationLandscapeLeft );
    }
Gunrunning answered 27/8, 2014 at 11:3 Comment(0)
C
0

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);
}
Craiova answered 7/4, 2015 at 6:42 Comment(0)
B
0

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.

Babyblueeyes answered 24/7, 2015 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.