views:

1027

answers:

4

I've got three ViewControllers set up to handle three views. The problem that I'm having is that in the simulator the orientation is LandscapeRight (which is what I want), and the first view shows up correctly in that landscape view, but when I move onto the second and third views, they show up rotated 90 degrees counter-clockwise with the upper-left corner of the view in the lower left corner of the phone's screen. I've been trying to debug this for a few days and the closest that I've gotten to a clue is tracing it the following way:

The following is in my app delegate's applicationDidFinishLaunching:

NSLog(@"1");
[window addSubview:welcomeController.view];
NSLog(@"2");
[window addSubview:goalController.view];
NSLog(@"3");
[window addSubview:planningController.view];
NSLog(@"4");

[window bringSubviewToFront:welcomeController.view];
NSLog(@"5");

Each of my ViewControllers implement something similar to the following (the only change being the controller's name switched out in the string passed to NSLog):

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
NSLog(@"called for WelcomeController");
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

With that, I get the following output on the Console:

a
called for WelcomeController
called for WelcomeController
called for WelcomeController
called for WelcomeController
2
called for GoalController
3
called for PlanningController
4
5

I find it interesting that shouldAutorotateToInterfaceOrientation is called 4 times for the first view that's added, while the other two only get called once. I expect that this is probably because it's got to do some setup at first (and I believe that the simulator starts off in portrait mode, so it's might be calling it while doing the rotation), but I find the correlation a bit suspicious.

I've switched the order around so that the addSubview is called for the goalController first and the welcomeController second. In this case, it's the goalController which displays in the correct landscape orientation (it's normally the welcome controller). This would seem to eliminate my XIB files and the ViewControllers themselves. I'm not sure why the first view where addSubview is called is special. I also tried using insertSubview at index 0 with the same results.

A: 

I had a similar issue. Not sure why either. But the workaround was to call this on every view after the first one:

[planningController.view setFrame:CGRectMake(0, 0, 480, 300)];

and before -addView. I'm curious if this helps you out. If I am not the only one with this problem and this workaround, then maybe there's a reason.

mahboudz
This didn't seem to have helped my code. I tried putting calls for both goalController and planningController before and after the corresponding addSubview calls (I even tried switching the 300 and 480 ;) It does seem to make a difference at some points because, instead having the welcome view in the background of the part of the screen that isn't covered, it's just white, so I think it must be stretching something across the screen.
Brian Underwood
One thing that I noticed from your post is that you seem to have a table view that is displaying in portrait mode, leaving the right approximately 1/3 of the page blank. I'm getting the same thing, except my view is rotated 90 degrees so that what should be the top is pointing left.
Brian Underwood
I designed everything in IB with the landscape orientation, which may be why things are pointing the right way, but not taking up the whole screen. I started with the mainWindow, to the viewControllers, and the views, all are in landscape.Like you, if I change the order of my view controllers, then whichever is first, doesn't need the setFrame call.
mahboudz
+2  A: 

Ran into the same problem, and apparently adding subviews to a UIWindow doesn't work the way I expected it to. I managed to solve the problem after adding a "dummy" UIViewController that is the ONLY subview in the UIWindow. After adding that one, it works perfectly to add multiple subviews to the dummy-controller, all with the correct orientation.

So the only code in the "dummy" controller class is the "shouldAutorotateToInterfaceOrientation" function. This should also match the same function in all the other subviews.

Hope it helps.

neer
This doesn't work for me (on iPad).I'm no noobsor, but this is seriously frustrating.
half_brick
A: 
half_brick
A: 

I think I have a solution for this that appears to work. Add a view, and then immediate remove it, repeat for each view, then add all three. Like this:

[window addSubview:welcomeController.view];
[welcomeController.view removeFromSuperview];
[window addSubview:goalController.view];
[goalController.view removeFromSuperview];
[window addSubview:planningController.view];
[planningController.view removeFromSuperview];

[window addSubview:welcomeController.view];
[window addSubview:goalController.view];
[window addSubview:planningController.view];

It seems to work, at least in the simulator.

Peter N Lewis