添加到根视图控制器时子视图不会自动调整大小

发布于 2024-08-28 16:45:41 字数 641 浏览 9 评论 0原文

我有一个根视图控制器,最多有 10 个左右的子视图。 我正在整个应用程序中实现自动旋转/自动调整大小。

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

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

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

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

  • 我错过了什么吗?任何帮助将不胜感激。我的项目是从 iPhone 应用程序移植到 iPad,iPhone 应用程序不会旋转,所以我不确定 3.2 beta 是否有问题。

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.

  • 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)

  • 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.

  • 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早茶月光 2024-09-04 16:45:41

经过一段时间的研究后,我将以下代码添加到我的子视图中。它在视图添加到根视图控制器后执行。到目前为止它似乎有效。

-

(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];


 }   
}

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];


 }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文