iPhone app in landscape mode, 2008 systems
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!
Landscape Mode ONLY for iPhone or iPad
Code found here
Launching in Landscape Mode
Applications in iPhone OS normally
launch in portrait mode to match the
orientation of the Home screen. If you
have an application that runs in both
portrait and landscape modes, your
application should always launch in
portrait mode initially and then let
its view controllers rotate the
interface as needed based on the
device’s orientation. If your
application runs in landscape mode
only, however, you must perform the
following steps to make it launch in a
landscape orientation initially.
In your application’s Info.plist file, add the
UIInterfaceOrientation
key and set its value to the
landscape mode. For landscape
orientations, you can set the value
of this key to
UIInterfaceOrientationLandscapeLeft
or
UIInterfaceOrientationLandscapeRight.
Lay out your views in landscape mode and make sure that their
autoresizing options are set
correctly.Override your view controller’s
shouldAutorotateToInterfaceOrientation:
method and return YES only for the
desired landscape orientation and NO
for portrait orientations.
Launching app in Landscape Mode
Instead of using [[UIScreen mainScreen] applicationFrame]
try using [[[[[self view] window] rootViewController] view] bounds]
The bounds will represent the width and height correctly in Landscape orientation, because the bounds will take into account the transform (rotation) that has been applied, while the frame will not.
To see what I mean, set a breakpoint, and in the debugger print out the description of the top level view lldb> po [[[[self view] window] rootViewController] view]
You'll see that the view has a rotation transform and that its frame does not represent the dimensions of the screen in landscape, but represents the dimensions in portrait!
The long way to calculate the correct applicationFrame would be
BOOL iOS7 = NO;
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending)
iOS7 = YES;
CGRect theFrame;
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
theFrame.origin = CGPointZero;
theFrame.size.width = screenBounds.size.height;
theFrame.size.height = screenBounds.size.width;
if (iOS7 == NO) {
// statusBarFrame will be CGRectZero if not visible, so this is safe
theFrame.size.height -= statusBarFrame.size.width; // because we're in landscape orientation
}
}
else {
theFrame = screenBounds;
if (iOS7 == NO) {
// statusBarFrame will be CGRectZero if not visible, so this is safe
theFrame.size.height -= statusBarFrame.size.height; // because we're in portrait orientation
}
}
Start app in landscape mode only and lock the application in landscape mode
You need to do this
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations;
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
How to change the orientation to landscape right if the device is iPad
See my previous post:
Always open a view in potrait mode in view in ipad?
Make app start and remain in landscape mode
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Related Topics
How to Detect the End of Loading of Uitableview
How to Play a Local Video With Swift
Uinavigationbar Custom Back Button Without Title
How to Handle Background Audio Playing While iOS Device Is Locked or on Another Application
Best Way to Check If Uitableviewcell Is Completely Visible
How to Make an Http Request + Basic Auth in Swift
Converting Hex String to Nsdata in Swift
Launching App or App Store from Safari
Displaying Splash Screen for Longer Than Default Seconds
How to Change Height of Navigation Bar - Swift 3
How to Parse Json Response from Alamofire API in Swift
Get the Current Scroll Position of a Swiftui Scrollview
How to Tell Swiftui Views to Bind to Nested Observableobjects
How to Receive Nsnotifications from Uiwebview Embedded Youtube Video Playback
How to Programmatically Check Whether a Keyboard Is Present in iOS App
Wkwebview Equivalent for Uiwebview's Scalespagetofit
How to Save an Array of Objects to Nsuserdefault with Swift
Custom Edit View in Uitableviewcell While Swipe Left. Objective-C or Swift