只有第一个UIView添加的视图addSubview显示正确的方向 [英] Only first UIView added view addSubview shows correct orientation

查看:109
本文介绍了只有第一个UIView添加的视图addSubview显示正确的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个ViewController设置为处理三个视图。我的问题是,在模拟器的方向是LandscapeRight(这是我想要的),第一个视图正确显示在该横向视图,但当我移动到第二个和第三个视图,他们显示与手机屏幕左下角视图的左上角逆时针旋转90度。我一直在试图调试这个几天,我已经得到一个线索的最接近的是跟踪它的以下方式:



以下是在我的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);

我的每个ViewController实现类似于以下内容(唯一的更改是控制器的名称该字符串传递给NSLog):

   - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES支持的方向
NSLog(@called WelcomeController);
return(interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}



这样,我在控制台上得到以下输出:

  a 
呼叫WelcomeController
呼叫WelcomeController
呼叫WelcomeController
呼叫WelcomeController
2
呼叫GoalController
3
呼叫PlanningController
4
5

我发现有趣的是,shouldAutorotateToInterfaceOrientation被第一次被添加的视图调用4次,而其他两个只被调用一次。我期望这可能是因为它必须做一些设置在第一(我相信模拟器开始在纵向模式,所以它可能是在调用它,而旋转),但我发现相关性有点可疑。 / p>

我已经切换了顺序,所以先调用goalController的addSubview,然后调用welcomeController。在这种情况下,它是以正确的横向方向显示的goalController(通常是欢迎控制器)。这似乎消除了我的XIB文件和ViewControllers本身。我不知道为什么addSubview被称为第一个视图是特殊的。我也尝试在索引0处使用insertSubview具有相同的结果。

解决方案

进入同样的问题, UIWindow 无法按我预期的方式工作。我设法解决这个问题后添加一个虚拟 UIViewController 这是UIWindow中的唯一子视图。添加一个后,它完美地添加多个子视图到虚拟控制器,所有的方向都正确。



因此,虚拟控制器类中唯一的代码是 shouldAutorotateToInterfaceOrientation 函数。



希望它有帮助。


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.

这篇关于只有第一个UIView添加的视图addSubview显示正确的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆