Note: this question is from 2008 and now is of only historic interest.
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.
From the Apple Dev Site:
Summary and integration from all the posts, after testing it myself; check the update for 4.x, 5.x below.
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:
UISupportedInterfaceOrientations
in theInfo.plist
fileshouldAutorotateToInterfaceOrientation:
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.Update (iOS 4.x, 5.x): the Apple iOS App Programming Guide has a "Launching in Landscape Mode" paragraph in the "Advanced App Tricks" chapter.
References:
Historic answer only. Spectacularly out of date:
Please note that this answer is now hugely out of date.
This post is only a historical curiosity.
Exciting news! As discovered by Andrew below, this problem has been fixed by Apple in 4.0+.
It would appear it is NO longer necessary to force the size of the view on every view, and the specific serious problem of landscape "only working the first time" has been resolved.
As of April 2011, it is not possible to test or even build anything below 4.0, so the question is purely a historic curiosity. It's incredible how much trouble it caused developers for so long!
Here is the original discussion and solution. This is utterly irrelevant now, as these systems are not even operable.
It is EXTREMELY DIFFICULT to make this work fully -- there are at least three problems/bugs at play.
try this .. interface builder landscape design
Note in particular that where it says "and you need to use shouldAutorotateToInterfaceOrientation properly everywhere" it means everywhere, all your fullscreen views.
Hope it helps in this nightmare!
An important reminder of the ADDITIONAL well-known problem at hand here: if you are trying to swap between MORE THAN ONE view (all landscape), IT SIMPLY DOES NOT WORK. It is essential to remember this or you will waste days on the problem. It is literally NOT POSSIBLE. It is the biggest open, known, bug on the iOS platform. There is literally no way to make the hardware make the second view you load, be landscape. The annoying but simple workaround, and what you must do, is have a trivial master UIViewController that does nothing but sit there and let you swap between your views.
In other words, in iOS because of a major know bug:
You can do that only once. Later, if you try to remove happyThing.view, and instead put in there newThing.view, IT DOES NOT WORK - AND THAT'S THAT. The machine will never rotate the view to landscape. There is no trick fix, even Apple cannot make it work. The workaround you must adopt is having an overall UIViewController that simply sits there and just holds your various views (happyThing, newThing, etc). Hope it helps!
I'm surprised no one has come up with this answer yet:
In all my tests when a dismissing a modal view controller the parent view controller's preferred orientation set in
shouldAutorotateToInterfaceOrientation
is honored even when part of a UINavigationController. So the solution to this is simple:Create a dummy UIViewController with a UIImageView for a background. Set the image to the default.png image your app uses on startup.
When
viewWillAppear
gets called in your root view controller, just present the dummy view controller without animation.when
viewDidAppear
gets called in your dummy view controller, dismiss the view controller with a nice cross dissolve animation.Not only does this work, but it looks good! BTW, just for clarification i do the root view controller's
viewWillAppear
like this:First I set in info.plist
then I put this code in applicationDidFinishLaunching:
This way I can work on the view in Interface Builder in landscape mode.
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...
Look for "Launching in Landscape Mode", page 102.