Only the first added UIView view addSubview shows the correct orientation

I have three ViewControllers configured to handle three views. The problem I am facing is that in the simulator the orientation is LandscapeRight (which is exactly what I want) and the first view is displayed correctly in this landscape view, but when I switch to the second and third views, they appear rotated to 90 degrees counterclockwise with the upper left viewing angle in the lower left corner of the phone screen. I tried to debug this for a few days, and the closest I realized is tracking it like this:

In the appDidFinishLaunching app, the following:

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 implements something similar to the following (the only change is the name of the controller, which is disabled on the line passed to NSLog):

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

With this, 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'm curious that shouldAutorotateToInterfaceOrientation is called 4 times for the first view added, while the other two are called only once. I expect that this is probably due to the fact that you need to do some tuning first (and I believe that the simulator runs in portrait mode, so it can cause it when performing a rotation), but I find the correlation a bit suspicious.

I switched the order around, so addSubview is called for firstController and secondController. In this case, it is the goalController, which is displayed in the correct landscape orientation (usually it is the receiving controller). It would seem that this excludes my XIB files and 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.

+7
iphone cocoa-touch
Sep 27 '09 at 23:38
source share
4 answers

Ran into the same problem, and obviously adding subviews to UIWindow does not work as I expected. I managed to solve the problem after adding the "dummy" UIViewController , which is ONLY a subview in UIWindow. After adding this, it works great to add multiple subzones to the dummy controller, all with the correct orientation.

Thus, the only code in the dummy controller class is the function shouldAutorotateToInterfaceOrientation . This should also match the same function in all other areas.

Hope this helps.

+4
Dec 15 '09 at 8:23
source share

I had a similar problem . I do not know why. But the workaround was to invoke this on every view after the first:

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

and up to -addView. I am wondering if this helps you. If I'm not the only one with this problem and this is a workaround, then there may be a reason.

0
Sep 28 '09 at 4:46
source share

This is far from ideal. But you can hack the second view so that it is rotated correctly. This works for me because my application only works in landscape mode. This may not be ideal if you want to change the 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)]; 

code>

0
Apr 22 '10 at 21:15
source share

I think I have a solution for this that seems to work. Add a view, and then immediately delete it, repeat for each view, and 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.

0
Jun 11 '10 at 6:25
source share



All Articles