Landscape only iPhone app with multiple nibs
Asked Answered
G

2

8

I'm developing an iPhone application that has several nibs, and should be landscape only.

The application is set to start in landscape mode via its Info.plist file.

I have two view controllers: FirstViewController and SecondViewController.

For each of these I have a nib file, where the view is in landscape. Both view controllers are added to my MainView nib as outlets, and their views are lazily initialized.

When the application loads, the first view displays in landscape, as expected. However, when I switch to the second view, the device (or simulator) remains in landscape, but the view is rotated, as if the device were in portrait mode, braking my interface.

In both UIViewController classes I have the following code:

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

and to switch views, in my app delegate I'm doing:

[viewController.view removeFromSuperview];
[window addSubview:secondViewController.view];

where viewController and secondViewController are the two outlets where the view controllers are connected.

This is how the second view looks in IB: alt text http://img27.imageshack.us/img27/4898/picture1ni.png

and this is how it looks in the simulator: alt text http://img402.imageshack.us/img402/4866/picture2wt.png

Why is that the second view is displaying in landscape but with the interface rotated?

I wouldn't like to deal with transform properties, since that seems overkill.

Goldia answered 11/12, 2009 at 22:50 Comment(0)
B
3

I starred this question hoping someone would give you an insightful response and I'd learn something.. sadly I'm afraid that you might need to use transforms to get this to work properly. Here's the code I've been using lately to solve the problem:

- (void)forceLandscapeForView:(UIView *)theView {
  theView.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
  theView.bounds = CGRectMake(0, 0, 480, 320);
  theView.center = CGPointMake(160, 240);
  [theView setNeedsLayout];
  [theView setNeedsDisplay];
}

Then when you're adding your new view, check the current orientation and if necessary force the rotation:

if (!UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
  [self forceLandscapeForView:_activeViewController.view];
}

Then of course you'll want to respond appropriately to shouldAutorotateToInterfaceOrientation in each of your view controllers:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

I would love to hear about alternate solutions if this isn't all necessary. There is also one caveat I've noticed with this setup: if you have a transition between views, and you rotate the phone during that transition, it's possible for the views orientations to get flipped or "stuck" on the wrong landscape orientation, such that you need to turn the phone over (landscape-right vs landscape-left) as you navigate between views.

Brendabrendan answered 15/12, 2009 at 18:56 Comment(2)
So far, this proved to be the most reliable approach to accomplish what I want.Goldia
This (the first method) worked for me. Been looking for a solution for hours and I tried something similar earlier (but within the views didLoad...). Apply the above transformation before adding it as a subView.Grime
S
0

it is just a suggestion but you can try to return NO in the shouldAotorotate method for the second view. Or try to make it in the portrait view in the IB. It seems that your view was loaded correctly(in the landscape mode) but then received shouldAutorotate message and had been rotated by 90 degrees.

Shepherd answered 12/12, 2009 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.