tags:

views:

16046

answers:

11

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.

+1  A: 

I don't have access to the iPhone Dev Center, but I've found this link on the web:

http://developer.apple.com/iphone/library/codinghowtos/UserExperience/index.html#GENERAL-START_MY_APPLICATION_IN_LANDSCAPE_MODE

Is this helpful?

saniul
For the record -- for anyone reading this. The answers before about 2009 are now (2010) completely irrelevant. Good luck with this annoying bug!
Joe Blow
A: 

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.

Michiel de Mare
A: 

As long as the iPhone SDK is under NDA answering questions about iPhone development will be next to impossible without violating said NDA.

Robert Höglund
This is true, and very, very annoying. Apple apparently did that at the beginning because the SDK wasn't stable, but I really wish they'd life that restriction now.
Ovid
A: 

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?

Jeremy White
+19  A: 

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>
Michael Pryor
This plus in your shouldAutorotateToInterfaceOrientation methods return (interfaceOrientation != UIInterfaceOrientationPortrait); and you're done.
mbehan
This is basically TOTALLY INCORRECT and it is very unfortunate for beginners reading here that this answer has 15 tick marks. The whole point of the question is that, famously, there are at least THREE major, famous, bugs that affect this issue. Johannes below makes exactly the right points. Johannes point 4 is one of the critical secrets, or just read the link to the other long forum about it, where it is fully explained.
Joe Blow
The Question asks how to start the device in landscape mode. The answers gives that information. Handling orientation after that is not mentioned in the question. +1 Michael Pryor
ing0
Intriguingly, the "answer" will in fact NOT start the device in landscape mode -!! It is probably the single most famous bug/difficulty in all of the Apple environment. It's a basic for very working iPad developer!
Joe Blow
A: 

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.

Sören Kuklau
A: 

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.

+3  A: 

A nice tutorial for setting the lanscape mode step by step:

http://www.dejoware.com/blogpages/files/iphone_programming_landscape_view_tutorial.html

zhongshu
+1  A: 

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.

Sam V
+3  A: 
Joe Blow
+4  A: 

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:

  1. set your orientation to UISupportedInterfaceOrientations in the Info.plist file
  2. in your window define a 480x320 "base view controller". Every other view will be added as a subview to its view.
  3. in all view controllers set up the shouldAutorotateToInterfaceOrientation: method (to return the same value you defined in the plist, of course)
  4. in all view controllers set a background view with

    self.view.frame = CGRectMake(0, 0, 480, 320)

    in the viewDidLoad method.

References:

IlDan
I can confirm this is the only, truly working solution (with a small addition). It took me hours to find and evaluate this. All other answers are only partial solutions to the problem.
Johannes Rudolph