An application for using only iPhone landscape templates

I am trying to create an iPhone application that is always in landscape mode using the Utility application template. Here is what I did:

  • Create a new iPhone application project using the utility application template
  • In the Builder interface, rotate all views 90 degrees.
  • In Interface Builder, add a label to the middle of the MainView. Stretch it all around, set the center alignment, and set the startup springs so that it can stretch horizontally.
  • In the Info.plist add the key "UIInterfaceOrientation" with the value "UIInterfaceOrientationLandscapeRight"
  • In controller classes, change the toAutorotateToInterfaceOrientation methods to "return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);"
  • Launch the app.

When I launch my application, it appears in landscape orientation, but the main view covers only the upper half of the display and stretches horizontally. I get the same results both in the simulator and on the real device. I saw it with versions 2.2 and 2.2.1 of the SDK.

I managed to solve the problem by adding the next step to the following:

  • Add "self.view.autoresizesSubviews = NO;" to the RootViewController method viewDidLoad after "[super viewDidLoad];".

If I do this, then it works as expected. But that sounds like a hack. Why is this necessary?

I do not think this is a transformation problem. All elements are drawn in the correct orientation and with the correct scaling. The problem is that the rectangles of the borders of the main view become funky. It seems that the height of the main species is reduced a little more than half, and the width increases by about 50%.

If I do the same many steps using a View-based application template rather than Utility, then everything will work as expected. So I'm sure the problem is with the way the Utility application manages its views.

Does anyone understand what is going on here?

+4
iphone landscape
Feb 08 '09 at 13:49
source share
3 answers

I was going to say that setting this key does not rotate your interface; you still need to lay out your content in landscape mode and rotate accordingly using CFAffineTransform - see "Launching in landscape mode" in the iPhone OS Programming Guide. When I was about to find the link for you, I found this comment: "To run the application based on the landscape controller in iPhone OS versions prior to v2.1, you need to apply a 90-degree rotation to the transformation of the root view applications in addition to all the previous steps "Prior to iPhone OS 2.1, view controllers did not automatically rotate their views based on the value of the UIInterfaceOrientation key. However, this step is not required in iPhone OS 2.1 and later."

So, if you are using pre-2.1, you need to add this code to your viewDidLoad method in your view controller. (Otherwise, can you post the code?)

-(void)viewDidLoad // After loading the view, transform the view so that the co-ordinates are right for landscape // As described in iPhone Application Programming Guide // Weird, I'm sure this used to be needed, but it doesn't now. The one in CardScrollViewController is needed though. { [super viewDidLoad]; CGAffineTransform transform = self.view.transform; CGPoint center = CGPointMake(kScreenHeight / 2.0, kScreenWidth / 2.0); // Set the center point of the view to the center point of the window content area. self.view.center = center; // Rotate the view 90 degrees around its new center point. transform = CGAffineTransformRotate(transform, (M_PI / 2.0)); self.view.transform = transform; } 
+3
Feb 08 '09 at 17:23
source share

Jane describes setting UIInterfaceOrientation to UIInterfaceOrientationLandscapeRight (or UIInterfaceOrientationLandscapeLeft) and the rotation parameters recommended in the documentation, but I used a slightly different code block (at the same end) in my root view controller:

 - (void)loadView { UIView *primaryView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; primaryView.backgroundColor = [UIColor clearColor]; // Start in landscape orientation, and stay that way UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationLandscapeRight) { CGAffineTransform transform = primaryView.transform; // Use the status bar frame to determine the center point of the window content area. CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame]; CGRect bounds = CGRectMake(0, 0, statusBarFrame.size.height, statusBarFrame.origin.x); CGPoint center = CGPointMake(60.0, bounds.size.height / 2.0); // Set the center point of the view to the center point of the window content area. primaryView.center = center; // Rotate the view 90 degrees around its new center point. transform = CGAffineTransformRotate(transform, (M_PI / 2.0)); primaryView.transform = transform; } self.view = primaryView; [primaryView release]; } 

In addition to this, I applied the following delegate method in the root view controller:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return ( (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)); } 

Finally, I came across strange glitches when the simulator did not work automatically, so I needed to implement the following delegate method in my UIApplicationDelegate:

 - (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration; { // This prevents the view from autorotating to portrait in the simulator if ((newStatusBarOrientation == UIInterfaceOrientationPortrait) || (newStatusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) [application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; } 

After that, my application was able to start from the landscape (on the right) and remain in this orientation under firmware 2.0 and in Simulator.

+2
Feb 08 '09 at 20:02
source share

Try setting the orientation property of the Landscape view in the knife. This property can be found on the 4th tab [Attributes Inspector] of the UIView Info View in simulated metrics.

0
Nov 11 '11 at 3:52
source share



All Articles