gpt4 book ai didi

iphone - 在 UIViewController loadView 中使用什么尺寸?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:48:19 25 4
gpt4 key购买 nike

我的容器 UIViewController 将添加另一个 CustomViewController 的 View 到它的 View 层次结构中。它首先创建 Controller ,然后调整其 View 的大小,最后将其添加到 View 中。

    //create the controller
customController = [[CustomViewController alloc] initWithNibName:nil bundle:nil];
[customController willMoveToParentViewController:self];
[self addChildViewController:customController];
[customController didMoveToParentViewController:self];

//size and position the controller's view
customController.view.frame = CGRectMake(10, 10, 100, 50);

//add the new controller's view
[self.view addSubview:customController.view];
[customController didMoveToParentViewController:self];

调整大小的部分是我的问题。

CustomViewControllerloadView 中以编程方式创建其 View ,其中添加了许多 subview 。由于 CustomViewController 并不真正知道它的 View 最终将如何调整大小/定位,因此它使用简单的 init 进行初始化(不提供框架)。

这对于简单的 UIView 来说效果很好,但是一旦我添加了 subview ,我就遇到了问题。由于框架是 CGRectZero,将这些 subview 放置在“loadView”中不会真正起作用。此外,一旦我设置了自动调整大小掩码以确保它正确缩放,最终结果是所有 subview 都会增长以占据整个 View 。可能是因为 subview 的坐标都是 View 框架的 100%(因为 View 的框架是 CGRectZero)。

简而言之,我应该在 loadView 中使用多大尺寸的框架?我是否应该只假设我希望 View 占据的典型 View 大小?或者是否有更好的方法允许我添加 subview 但推迟 Controller View 的大小?

最佳答案

UIViewController 的 View 在 viewWillAppear 方法(UIViewController 的生命周期方法)中具有实际大小。所以你可以继承 UIViewController 类并在那里布局你的 subview 。

但是,我想给你一些建议。您应该使用 View Controller 的 loadView 方法来创建它的 View 。否则你可能会遇到内存管理问题和 View 布局问题。在 init 方法中调用 View 属性不是一个好主意,因为它会自动调用 UIViewController 的 loadView 方法。

阅读有关 UIViewController 生命周期的更多信息。 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

我的建议:使用父 UIViewController 的 loadView 来创建它的 View 。viewWillAppear 中的布局 View 。

祝你好运!有问题再问:)

关于iphone - 在 UIViewController loadView 中使用什么尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12716415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com