What's the best way to create an iPhone application that runs in landscape mode from the start, regardless of the position of the device? Both programmatically and using the Interface Builder.
I don't have access to the iPhone Dev Center, but I've found this link on the web:
Is this helpful?
If I use that setting, my app starts in portrait mode, but the simulator starts in landscape mode.
I've already checked out the apple forums, and there are some helpful suggestions, but I'm still looking for the canonical explanation of everything to do with portrait/landscape mode and autorotation.
As long as the iPhone SDK is under NDA answering questions about iPhone development will be next to impossible without violating said NDA.
Though you're still having issues, I think sasb's answer is correct. I've noticed several derivations in behavior between the iPhone SDK documentation, simulator, and actual iPhone device. There is a possibility that this is an issue with the iPhone OS. What version are you running?
From the Apple Dev Site:
To start your application in landscape mode so that the status bar is in the appropriate position immediately, edit your Info.plist file to add the UIInterfaceOrientation key with the appropriate value (UIInterfaceOrientationLandscapeRight or UIInterfaceOrientationLandscapeLeft), as shown in Listing 2.
Listing 2: Starting your application in landscape mode
<key>UIInterfaceOrientation</key> <string>UIInterfaceOrientationLandscapeRight</string>
sasb's and michaelpryor's answer appears to be correct, but if it's not working for you, try this alternative:
- (void)applicationDidFinishLaunchingUIApplication *)application {
application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
}
Or this one:
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
Or… this one:
[application setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];
You may also have to call window makeKeyAndVisible;
first.
A few links: Developing in landscape mode, iPhone SDK: How to force Landscape mode only?
@Robert: please refer to The iPhone SDK, NDA, and Stack Overflow.
The latest iPhone OS Programming Guide has a full section on this, with sample code. I am sure this is a recent addition, so maybe you missed it. It explains all the conditions you have to comply with; basically...
- set the Info.plist properties (this changes the position of the status bar, but not the view)
- rotate your view manually around its center, on either your UIViewController viewDidLoad: method or your applicationDidFinishLaunching: method or implement auto rotation ("Autoresizing behaviors", page 124)
Look for "Launching in Landscape Mode", page 102.
A nice tutorial for setting the lanscape mode step by step:
http://www.dejoware.com/blogpages/files/iphone_programming_landscape_view_tutorial.html
First I set in info.plist
<key>UIInterfaceOrientation</key>
<string>UIInterfaceOrientationLandscapeRight</string>
then I put this code in applicationDidFinishLaunching:
CGAffineTransform rotate = CGAffineTransformMakeRotation(1.57079633);
[window setTransform:rotate];
CGRect contentRect = CGRectMake(0, 0, 480, 320);
window.bounds = contentRect;
[window setCenter:CGPointMake(160.0f, 240.0f)];
This way I can work on the view in Interface Builder in landscape mode.
Summary and integration from all the posts, after testing it myself.
As of 3.2 you cannot change the orientation of a running application from code.
But you can start an application with a fixed orientation, although doing so this is not straightforward.
Try with this recipe:
- set your orientation to
UISupportedInterfaceOrientations
in theInfo.plist
file - in your window define a 480x320 "base view controller". Every other view will be added as a subview to its view.
- in all view controllers set up the
shouldAutorotateToInterfaceOrientation:
method (to return the same value you defined in the plist, of course) in all view controllers set a background view with
self.view.frame = CGRectMake(0, 0, 480, 320)
in the
viewDidLoad
method.
References: