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.


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.


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.


This is far far far from ideal. But you can hack the second views transform so it is rotated correctly. This works for me because my app is only ever in landscape mode. It may not be ideal if you want to change orientation.

[window addSubview:firstController.view];
[window addSubview:secondController.view];
CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI/2.0);
[backgroundViewController.view setTransform:rotate];
CGRect contentRect = CGRectMake(0, 0, 1024, 768); 
backgroundViewController.view.bounds = contentRect; 
[backgroundViewController.view setCenter:CGPointMake(768/2, 1024/2)];

链接地址: http://www.djcxy.com/p/43296.html

上一篇: Access编程的最佳实践

下一篇: 只有第一个UIView添加视图addSubview显示正确的方向