Programatic iPhone landscape orientation lock - iphone

The iPhone has a orientation lock for portrait only. So I added a landscape orientation lock button to my UI that sets a landscape_orientation_locked variable to YES or NO. Then in shouldAutorotateToInterfaceOrientation: I basically do the following:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (landscape_orientation_locked)
return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
return YES;
}
This works fine, except for one case. When I turn the iPhone to landscape orientation, toggle the orientation lock button, rotate the iPhone to portrait orientation and hit the button again. The interface orientation won't change from landscape to portrait. The iPhone has to be turned into landscape and then portrait orientation to trigger the interface rotation.
So my question is: Can I somehow 'force' the iPhone to re-evaluate it's current orientation?

Presenting and then dismissing a mock modal view controller should force an orientation check.
EDIT: Found the question where I first read about that:
Is there a documented way to set the iPhone orientation?

Related

Universal project: how to make landscape start-up in iPhone and portrait in iPad

I have an universal project, and want to keep landscape from very beginning of star-up for iPhone and portrait for Pad. How can I do it?
That's not possible. iPhone apps always have to start in Portrait orientation. Any game you see that has landscape-only display is still starting showing a portrait default image and the root view controller then can be landscape-only.
On iPad you can restrict the app orientation to Landscape and also have it start in Landscape.
The iPhone restriction is enforced by Apple and they won't approve an iPhone app that restricts itself to Landscape start.
If the device is already in landscape position it will start as landscape, but if you want your program to work always in landscape position and never flip to portrait, you have to set the orientation to landscape in each class you create:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscape || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

ask iphone device orientation to refresh

I'm creating a puzzle game for the iphone. All of my menus, etc are only in the portrait orientation, but my gameplay can be in either portrait or landscape.
So when I'm in the menu and the device is being held in landscape, then when I go to the gameplay viewcontroller, it does the layout with the landscape coordinates, but the screen is still actually oriented portrait-wise:
I want to force the device to re-orient the screen before I do the layout. How do I do that?
right now I'm testing for device orientation like this:
- (void) viewDidLoad {
.
.
.
if (!UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
// Run Portrait Layout
}
else {
// Run Landscape Layout
}
}
So I know that the device is returning an orientation of landscape, but the screen is still oriented portrait for some reason.
Perhaps try..
[UIViewController attemptRotationToDeviceOrientation];
In viewDidLoad or viewWillAppear, that should make it attempt an orientation change
There are a few methods in UIViewController that you can override to catch the view-did-rotate event:
– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:
Do check the UIViewController class reference

Forcing a UIVIew to lay itself out for the current device orientation

I've been working on this for days and can't crack it. The sequence of events is:
In landscape (let's just say right), the user hits an "edit" button.
A portrait only modal interface slides in. Its shouldAutorotateToInterfaceOrientation returns yes only for portrait.
The device becomes convinced it is in portrait mode.
Problem 1: If the user dismisses the interface without having actually rotated to portrait mode, the device reports that is in landscape right orientation (which it is), but the interface is laid out in portrait orientation.
Problem 2: If the user rotates to portrait, the interface does not get laid out again.
Problem 3: (And this one is weird) Rotating back to the same landscape orientation (right) where the edit button was pressed causes the status bar to return to landscape, but nothing else changes. All my stuff remains laid out in portrait mode. Rotating to the other (left) landscape orientation works perfect.
I need some way to force the layout engine to redo the layout for the orientation the device is actually in.
UIView - layoutSubviews, - setNeedsLayout, and -layoutIfNeeded don't have any effect.
Thanks for any help.
Do what iBooks (2.x) does and force the interface orientation to portrait with
+ (void)attemptRotationToDeviceOrientation. This way, you nip the problem in the bud at step one.

iphone - programmatically rotate system keyboard to landscape when device is in portrait orientation

I would like to do this because the keyboard buttons are slightly bigger in landscape mode than portrait and I will have older users using my app.
Does anybody know a way to do it??
Thanks.
The keyboard will show up in the orientation of the statusbar, which doesn't always change with rotations for some reason. So if you want to display that view only in landscape set [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft; or whichever orientation you want to prefer in your viewWillAppear method.

UIDEVICE orientation

hello all i have my splash screen which i want to show in landscape mode and all other screen in potrait mode. My root view controller is acting as a splash screen i am writing this code in viewdidload method
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
But this code is not working and shows me that UIdevice may not respond to setorentation please guide me that how could i change the orentation for only first splash screen and also other screen should be in potrait mode
UIDeviceOrientation refers to the physical orientation of the device whereas UIInterfaceOrientation refers to the orientation of the user interface. You can't change the physical orientation of the device, but you can change how the status bar is displayed via [UIApplication sharedApplication].statusBarOrientation
Can't you just have a splash screen that is displayed sideways?
The user will interpret that as a "landscape" splash and adjust accordingly, whether or not your application actually thinks it is. Just make sure to return NO in shouldAutorotateToInterfaceOrientation: so it doesn't rotate away when the user turns it.
But a warning: it's probably worthwhile to tweak your splash screen so it is in the portrait orientation, since it may be annoying to suggest to the user that your app likes landscape, and then switch to portrait as soon as it actually starts.
If you really want to, though, you can use setStatusBarOrientation:animated: on the UIApplication to force the orientation.