Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only first UIView added view addSubview shows correct orientation

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.

like image 247
Brian Underwood Avatar asked Sep 27 '09 23:09

Brian Underwood


1 Answers

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.

like image 168
neer Avatar answered Sep 29 '22 19:09

neer