UIViewController 在 vi​​ewWillAppear 和 viewDidAppear 之间调整自身大小?

发布于 2024-11-05 16:13:59 字数 1352 浏览 0 评论 0原文

我的项目中有一个非常奇怪的错误。我有一个 UIScrollView 作为我的主要大视图。在它的内部,我有一个 UIViewController (不是 UITableViewController),它有一个 UITableView 实例变量,以及一些杂项 UIButtons< /代码>。我已将视图控制器的视图框架设置为 CGRectMake(0, 64, 320, 388),因为它上面有一个选项卡栏(这还不起作用)。起初,它工作得很好,看起来也很棒,但是一旦我呈现并关闭了 modalViewController (我相信,因此重新加载了 UIViewController),它就会推送 UIViewController code> 的视图到屏幕顶部(默认设置为 CGRectMake(0, 0, 320, 460) ,但由于我设置了 wantsFullScreenLayoutNO,现在将其设置为 CGRectMake(0, 0, 320, 388)

我已将此问题跟踪到 viewWillAppear 之间的某个位置。和 viewDidAppear ,这是我关闭 modalViewController 后的确切日志:

2011-05-06 11:08:39.974 Campus[1570:207] 框架为 0.000000、64.000000、320.000000、388.000000 (viewWillAppear)
2011-05-06 11:08:40.378 Campus[1570:207] 框架为 0.000000、0.000000、320.000000、388.000000 (viewDidAppear)

如您所见,框架在 viewWillAppear 中很好,但在viewDidAppear

我已执行以下操作来尝试修复该问题:
- 在 loadViewviewDidLoadviewWillAppearviewDidAppear 中设置所需的框架。
- 将我的 wantsFullScreenLayout 设置为 NO
- 杀死了我的方法覆盖中的 [super viewWillAppear:][super viewDidAppear:] 调用。

我应该怎么办?!?!?

I've got a really strange bug in my project. I've got a UIScrollView as my main, big view. Inside of it, I have a UIViewController (not UITableViewController) which has a UITableView instance variable, as well as some miscellaneous UIButtons. I have set the view controller's view frame to CGRectMake(0, 64, 320, 388), as I have a tab bar above it (this is not functional yet). At first it works great and looks great, but once I present and dismiss a modalViewController (thus reloading the UIViewController, I believe), it pushes the UIViewController's view to the top of the screen (by default setting it to CGRectMake(0, 0, 320, 460), but since I've set wantsFullScreenLayout to NO, it now sets it to CGRectMake(0, 0, 320, 388).

I've tracked this problem to somewhere between viewWillAppear and viewDidAppear. Here are my exact logs after dismissing the modalViewController:

2011-05-06 11:08:39.974 Campus[1570:207] Frame is 0.000000, 64.000000, 320.000000, 388.000000 (viewWillAppear)
2011-05-06 11:08:40.378 Campus[1570:207] Frame is 0.000000, 0.000000, 320.000000, 388.000000 (viewDidAppear)

As you can see, the frame is fine in viewWillAppear, but not in viewDidAppear.

I've done the following things to try to fix it:
- Set the desired frame in loadView, viewDidLoad, viewWillAppear, and viewDidAppear.
- Set my wantsFullScreenLayout to NO.
- Killed my [super viewWillAppear:] and [super viewDidAppear:] calls in my method overrides.

What should I do?!?!?

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

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

发布评论

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

评论(1

如日中天 2024-11-12 16:13:59

我的问题是,从 iOS 4 开始,Apple 只支持每个窗口 1 个视图控制器。然而,从 iOS 5 开始,Apple 添加了对容器视图控制器的支持,并向 UIViewController 添加了方法,例如 addChildViewController:。使用容器视图控制器算法解决了我的问题。请访问 UIViewController 类参考更多信息。

编辑:对于那些懒得在类参考中搜索“容器视图控制器”的人,这里是类参考中相关部分的要点:

自定义 UIViewController 子类也可以充当容器视图
控制器。容器视图控制器管理以下内容的呈现
它拥有的其他视图控制器的内容,也称为它的子视图
视图控制器。孩子的观点可以按原样呈现,也可以按原样呈现
与容器视图控制器拥有的视图结合使用。

你的容器视图控制器子类应该声明一个public
关联其子项的接口。这些方法的性质是
对您来说,取决于您正在创建的容器的语义。
您需要决定您的视图可以显示多少个孩子
立即控制器,这些子项何时显示,以及它们在哪里
出现在视图控制器的视图层次结构中。你的视图控制器
类定义子级共享的关系(如果有)。
通过为您的容器建立一个干净的公共接口,您
确保孩子们能够逻辑地使用其功能,而无需访问
关于容器如何实现的太多私人细节
行为。

您的容器视图控制器必须关联子视图控制器
在将子视图的根视图添加到视图层次结构之前。
这允许 iOS 正确地将事件路由到子视图控制器并
这些控制器管理的视图。同样,删除后
子视图与其视图层次结构的根视图,它应该断开连接
子视图控制器本身。成就或毁掉这些
关联,您的容器调用由
基类。这些方法不适合由以下客户端调用
你的容器类;它们只能由您的容器使用
实现以提供预期的遏制行为。

My problem here was that, as of iOS 4, Apple only supported 1 view controller per window. However, as of iOS 5, Apple has added support for container view controllers, and has added methods to UIViewController such as addChildViewController:. Using the container view controller algorithm solved my problem. Visit the UIViewController Class Reference for more info.

EDIT: for those of you too lazy to search "container view controller" in the class reference, here's the gist of the related section in the class reference:

A custom UIViewController subclass can also act as a container view
controller. A container view controller manages the presentation of
content of other view controllers it owns, also known as its child
view controllers. A child’s view can be presented as-is or in
conjunction with views owned by the container view controller.

Your container view controller subclass should declare a public
interface to associate its children. The nature of these methods is up
to you and depends on the semantics of the container you are creating.
You need to decide how many children can be displayed by your view
controller at once, when those children are displayed, and where they
appear in your view controller’s view hierarchy. Your view controller
class defines what relationships, if any, are shared by the children.
By establishing a clean public interface for your container, you
ensure that children use its capabilities logically, without accessing
too many private details about how your container implements the
behavior.

Your container view controller must associate a child view controller
with itself before adding the child’s root view to the view hierarchy.
This allows iOS to properly route events to child view controllers and
the views those controllers manage. Likewise, after it removes a
child’s root view from its view hierarchy, it should disconnect that
child view controller from itself. To make or break these
associations, your container calls specific methods defined by the
base class. These methods are not intended to be called by clients of
your container class; they are to be used only by your container’s
implementation to provide the expected containment behavior.

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