添加到根视图控制器时,子视图不会自动调整大小 [英] Subview Doesnt AutoSize When Added to Root View Controller

查看:156
本文介绍了添加到根视图控制器时,子视图不会自动调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个根视图控制器,最多可包含10个子视图。
我在整个应用程序中实现autorotation / autosize。

I have a root view controller that will have up to 10 or so subviews. I am implementing autorotation/autosize accross the entire app.

我的问题是:
- 当我分配所有视图控制器并添加每个作为启动期间根控制器的子视图,一切都按预期工作。唯一的问题是每个视图控制器都需要时间来初始化。这会导致我的应用程序加载速度非常慢。

My problem is this: - When I allocate all the view controllers and add each as a subview to the root controller during startup, everything works as it should. The only problem is that each view controller needs time to initialize. This causes my application to load very slowly.


  • 相反,我正在尝试根据需要分配视图控制器。现在我发现如果应用程序进入Landscape,并且我分配了一个纵向设计的视图控制器,它将自动旋转,但自动调整不会发生。

  • Instead I am trying to allocate the view controllers as they are required. Now I find that if the application goes into Landscape, and I allocate a view controller that is designed in portrait, it will autorotate but the autosize doesnt happen.

换句话说,一旦子视图以纵向模式添加到根控制器,它就会正确旋转并调整大小(并保持这种方式)。如果在根控制器处于横向状态时添加子视图,则它会旋转但不会自动调整大小(并且视图大小仍然会混乱,然后旋转回到肖像)

In other words as soon as the subview is added to the root controller in portrait mode it rotates and sizes correctly (and stays that way). If the subview is added when the root controller is in landscape it rotates but doesnt autosize (and view sizes remain messed up rotating back to portrait)

我尝试过通过调用SetNeedsLayout,SetNeedsDisplay和LayoutIfNeeded强制自动调整大小但没有任何效果。我知道我可以通过确定根控制器方向并适当调整子视图的大小来手动执行此操作,但这对于应该自动运行的事情来说是很多工作。

I have tried to force an autosize by calling SetNeedsLayout, SetNeedsDisplay, and LayoutIfNeeded but nothing works. I know i could probably do this manually by determining the root controllers orientation and resizing the subviews appropriately, but this is a lot of work for something that should work automatically.

我错过了什么吗?任何帮助,将不胜感激。我的项目是iPhone应用程序的iPad端口,iPhone应用程序不旋转所以我不确定这可能是3.2 beta的错误。

Am I missing something? Any help would be appreciated. My project is an iPad port from an iPhone app, the iPhone app doesnt rotate so Im not sure if this may be something wrong with the 3.2 beta.

推荐答案

在与此摔跤了一段时间后,我将以下代码添加到我的子视图。它在视图添加到根视图控制器后执行。到目前为止似乎有效。

After wrestling with this for a while I added the following code to my subview. It executes after the view is added to the root view controller. So far it seems to work.

-

(void) AdjustFrame{
 UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
 if((interfaceOrientation == UIInterfaceOrientationLandscapeLeft)||(interfaceOrientation == UIInterfaceOrientationLandscapeRight))
 {
  CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
  CGRect newFrame =  CGRectMake(0.0, 0.0, applicationFrame.size.height, applicationFrame.size.width);
  [self.view setFrame:newFrame];
  [self.view setNeedsDisplay];


 }   
}

这篇关于添加到根视图控制器时,子视图不会自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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