将UIViewController.view添加到另一个视图会导致方向问题 [英] Adding UIViewController.view to another view causes orientation problems

查看:120
本文介绍了将UIViewController.view添加到另一个视图会导致方向问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短版本:

我在一个UIViewControllers viewDidLoad方法中分配/初始化/保留一个新的UIViewController,将新视图添加到self.view。这通常有效,但似乎弄乱了我的iPad应用程序的方向更改处理。

Short version:
I'm alloc/init/retaining a new UIViewController in one UIViewControllers viewDidLoad method, adding the new View to self.view. This usually works, but it seems to mess up orientation change handling of my iPad app.

更长的版本

我正在构建一个相当复杂的iPad应用程序,涉及很多视图和视图控制器。在调整到设备方向遇到一些困难之后,我做了一个简单的XCode项目来找出问题所在。

Longer version:
I'm building a fairly complex iPad application, involving a lot of views and viewcontrollers. After running into some difficulties adjusting to the device orientation, I made a simple XCode project to figure out what the problem is.

首先,我已阅读有关此主题的Apple文档(一个名为为什么我的UIViewController不会随设备一起旋转?),虽然我确信它与列出的原因之一有关在那里,我不确定如何解决它。

Firstly, I have read the Apple Docs on this subject (a small document called "Why won't my UIViewController rotate with the device?"), and while I do believe it has something to do with one of the reasons listed there, I'm not really sure how to fix it.

在我的测试项目中,我有一个appDelegate,一个rootViewController和一个带有两个自定义viewControllers的UISplitViewController。我使用rootViewController上的一个按钮切换到splitViewController,从那里我可以使用一个按钮切换回rootViewController。到目前为止一切都很好,即所有视图都调整为设备方向。

In my test project I have an appDelegate, a rootViewController, and a UISplitViewController with two custom viewControllers. I use a button on the rootViewController to switch to the splitViewController, and from there I can use a button to switch back to the rootViewController. So far everything is great, i.e. all views adjust to the device orientation.

但是,在splitViewController的右侧viewController中,我使用viewDidLoad方法初始化其他一些viewControllers ,并将他们的观点添加到自己的视图中:

However, in the right viewController of the splitViewController, I use the viewDidLoad method to initialize some other viewControllers, and add their views to its own view:

self.newViewController = [[UIViewController new] autorelease];
[newViewController.view setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:newViewController.view];

self.newViewController = [[UIViewController new] autorelease]; [newViewController.view setBackgroundColor:[UIColor yellowColor]]; [self.view addSubview:newViewController.view];

这是出错的地方。不知何故,添加此视图后,调整到设备方向是混乱的。在启动时一切都很好,在我切换到splitViewController之后一切都还可以,但是一旦切换回rootViewController它就会全部结束。我已经尝试过(几乎)有关保留和释放viewcontroller的所有内容,但似乎没有什么能解决它。

This is where things go wrong. Somehow, after adding this view, adjusting to device orientation is messy. On startup everything is fine, after I switch to the splitViewController everything is still fine, but as soon as I switch back to the rootViewController it's all over. I have tried (almost) everything regarding retaining and releasing the viewcontroller, but nothing seems to fix it.

从上面的代码可以看出,我已经将newViewController声明为属性,但如果不这样做,也会发生同样的情况。

As you can see from the code above, I have declared the newViewController as a property, but the same happens if I don't.

我不应该将ViewController的视图添加到我自己的视图中吗?这真的搞砸了我的项目,因为我有很多viewControllers在做各种各样的事情。

Shouldn't I be adding a ViewController's view to my own view at all? That would really mess up my project, as I have a lot of viewControllers doing all sorts of things.

对此的任何帮助将不胜感激......

Any help on this would be greatly appreciated...

推荐答案

我遇到了同样的问题,通过从父视图中删除IB中的autoresize子视图选项来解决接缝。

选择视图然后:检查器 - >属性(第一个选项卡) - >绘图。

I had the same problem, seams resolved by removing from the parent view the "autoresize subview" option in IB.
Select the view a then: Inspector -> Attributes (first tab) -> drawing.

这篇关于将UIViewController.view添加到另一个视图会导致方向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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