iPhone app in landscape mode, 2008 systems
Asked Answered
E

8

100

Please note that 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.

Evangelin answered 2/8, 2008 at 10:47 Comment(1)
For any future readers, please note that this famous problem is now SOLVED by Apple as of 2011. Note the test project in the answer below.Pinchas
P
47

Historic answer only. Spectacularly out of date.

Please note that this answer is now hugely out of date/

This answer 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:

[window addSubview:happyThing.view];
[window makeKeyAndVisible];

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!

Pinchas answered 27/3, 2010 at 21:13 Comment(2)
I believe this has been fixed in 4.x. We shipped a game that had a bunch of different screens, some landscape and some portrait, and it just didn't work in 3.x. Not wanting to get into the extra dummy view to handle it, we instead required 4.x -- which, a year after this question was asked, is probably OK.Bernardinebernardo
We didn't do "always landscape" but rather variously landscape or portrait. Each VC is directly added to and removed from the window. App launches landscape, stays that way through several screen transitions. We don't use a dummy view, but we DO override shouldAutorotate in every screen. No problems on 4.x, from touch to pad to phone.Bernardinebernardo
G
40

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>
Granddad answered 31/8, 2008 at 14:38 Comment(4)
This plus in your shouldAutorotateToInterfaceOrientation methods return (interfaceOrientation != UIInterfaceOrientationPortrait); and you're done.Virgate
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 PryorHornwort
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!Pinchas
@mbehan There is also UIInterfaceOrientationPortraitUpsideDown, hence not a fix. Additionally, UIInterfaceOrientation in .plist is supported only on 3.2+, not on 3.1.3-and-older devices (and remember iPhone 2G is stuck on that version).Cuneo
I
28

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:

  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.

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:

Immerse answered 7/5, 2010 at 11:2 Comment(1)
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.Elwandaelwee
K
8

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.

Klopstock answered 20/11, 2009 at 20:58 Comment(0)
U
7

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.

Undamped answered 31/8, 2008 at 15:1 Comment(0)
A
6

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:

- (void)viewWillAppear:(BOOL)animated
{
 if ( dummy != nil ) {
  [dummy setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
  [self presentModalViewController:dummy animated:NO];
  [dummy release];
  dummy = nil;
 }
...
}
Adamik answered 27/11, 2010 at 7:38 Comment(0)
G
5

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.

Galla answered 4/9, 2008 at 21:57 Comment(0)
T
4

See this answer: Landscape Mode ONLY for iPhone or iPad

  1. add orientation to plist
  2. shouldAutorotateToInterfaceOrientation = YES in all files

Although if you're using mixed modes, you might be better off with

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
Tarrsus answered 6/3, 2011 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.