OGeek|极客世界-中国程序员成长平台

标题: iphone - UIViewController 包含方向更改 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 14:34
标题: iphone - UIViewController 包含方向更改

我有一个 UIViewController 可以说它叫做 A。在 A 中,我添加了另一个名为 B 的 UIViewController。我还在 A 上添加了另一个 UIViewController,我们称之为 C。我遇到的问题是,当 C 不是存在(即:未添加到 A 的 View Controller )然后旋转给了我正确的大小,但是当 C 添加到 A 时。然后 UIViewContoroller 的 B 没有收到正确的帧调整。为什么会这样,我该如何解决?

基本上,当我在 B 的 willAnimateToInterfaceRotation 中旋转时,无论方向如何,框架都保持不变。所以说B是纵向的,尺寸是768 x 1004。然后我有一个在A中添加C的按钮(C在B前面)。当我旋转它时,它打印出框架的大小是 768x1004。但是,如果我关闭 C(即:从父 View Controller 中删除它,在本例中为 A)。当我旋转框架时,宽度会相应改变。



Best Answer-推荐答案


更新:

我意识到我可能误解了最初的问题。但要正确回答这个问题,我需要澄清你在说什么。所以,你说:

I have a UIViewController lets say it's called A. Inside A, I've added another UIViewController called B. I am also adding another UIViewController on A, lets call it C.

那么,从本质上讲,您已经完成了以下操作?

[A addChildViewController:B];
[B didMoveToParentViewController:A];
[A addChildViewController:C];
[C didMoveToParentViewController:A];

我说对了吗?不管怎样,你继续说:

The issue I am having is, when C is not present (i.e: not added to A's view controller) then the rotation gives me the right size,

那么,你是说你从来没有调用过 addChildViewController:C 来表示一切正常吗?你继续:

... but when C is added to A. Then the UIViewController's B did not receive the correct frame adjustment.

那么,您是说如果您调用了 addChildViewController:C,并且有 transitionFromViewController:B toViewController:C?但如果是这样的话,B 根本不会得到任何轮换事件。当您从 B 过渡到 C 时,引用 B.view.frame.size 对我来说没有意义。无论如何,这是我原始答案的基础,假设您必须一直在谈论以下场景:

因此,如果您需要 B 在这种情况下检查基于旋转的逻辑,则必须在 B 的 viewWillAppear 之类的方法期间执行此操作(除非父 View Controller A 显式调用其他内容在 transitionFromViewController:C toViewController:B 之后。但我现在意识到你可能并不关心旋转事件的存在与否,而只是当你检查框架时,你会得到奇怪的结果。但我不太明白你描述的场景,所以让我们继续你的问题。

所以,你继续说:

Basically when I rotate in B's willAnimateToInterfaceRotation the frame stays the same regardless of orientation. So say B is in portrait and the size is 768 x 1004.

好的,我认为我们在那里很好。你继续:

I then have a button that adds in C in A (C is in front of B).

你是说你做了transitionFromViewController:B toViewController:C?在 View Controller 包含的上下文中,术语“在 B 之前”对我来说没有意义。 C 不会走在 B 前面。你从 B 过渡到 C。

When I rotate it prints out the size of the frame is 768x1004.

这很奇怪。但是,更重要的是,“768x1004”是什么意思? C.view.frame.size?如果您从 B 过渡到 C,我认为引用 B.view.frame.size 是没有意义的。我不清楚你在说什么报告“768x1004”。

不管怎样,你继续:

However, if I dismiss C (i.e: remove it from the parent's view controller, in this case A). When I rotate the frame width changes accordingly.

当您说“从父 View Controller 中删除它”时,我假设您的意思是:

如果您能就我上面的解释(或误解)澄清您最初的问题,那么我们确切地知道您在做什么?我确实在容器中看到了一些关于框架尺寸的奇怪东西,但我想确保我首先理解你的问题。

原文:

automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers .说真的,这个方法名是谁想出来的?!?

但这不会将方法发送到非事件 View Controller 。看起来您必须对子 Controller 的 viewWillAppear 进行一些方向检查。

注意,如果不是预先加载所有 Controller ,而是及时加载(有点像标签 View Controller ),这个问题就会消失。

关于iphone - UIViewController 包含方向更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11380220/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4