- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的容器 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];
调整大小的部分是我的问题。
CustomViewController
在 loadView
中以编程方式创建其 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/
在 Matt Newburg 的 Programming iOS 4 中他说: “To provide a UIViewController with a view manually, impleme
我的 View 层次结构位于几个自定义“根”UIViewController 子类上。我正在尝试为我的根 VC 子类之一设置自定义 self.view。在那里,我正在做: MyRootViewCont
我的应用中有两个 ViewController ViewController1.m和 ViewController2.m . 在 AppDelegate我有这个代码。 self.window = [[
如果我在 UIViewController 中有一个复杂的 View 层次结构,那么何时将主视图分解到它自己的类中是合适的,即使它不能在其他地方重用?如果我要这样做,对于该 View 上的按钮,正确的
我有一个 UIViewController 使用定义的 loadView 方法调用另一个 View Controller 。我尝试了很多方案都没有成功解决 loadView 方法未被调用的问题。 感谢
我在 viewDidUnload 中将在 loadView 中创建的所有 subview 设置为 nil。 我在模拟器中模拟内存警告,并且我的屏幕外 View 及其 subview 通过viewDid
重写 UIViewController 中的 loadView 方法时,应该在方法的开头调用 [super loadView] 还是在方法的末尾调用?为什么? 最佳答案 根据UIViewControl
在我以编程方式启动的 View 之一中,我看到对“loadView/viewdidLoad”的 5 个不同调用,但我不明白为什么会调用这么多。有人可以向我解释一下这背后的机制吗? 我通过以下方式在父
我正在尝试完成基本的 iPhone 编程,并且我对 Interface Builder 的工作原理有很好的基本了解,因此我决定尝试以编程方式制作 View 。我已经浏览了 ViewController
使用 nib 创建的 View Controller 中的 View 的框架位于 {0, 64, 320, 416},原点从导航栏正下方开始。然而,在没有 Nib 的 -loadView 中,尽管具有
我正在尝试将 visualEffect 添加到 View 中,该 View 在 iOS10 和之前的版本上运行良好……但在 iOS 11 上它崩溃了。错误原因如下: has been added a
在 UIViewController 中使用 loadView 时非常有趣的问题。 通常我们是这样使用的 // Implement loadView to create a view hierarch
Apple 的文档没有说明 loadView 的正确实现方式。 我发现如果你像这样实现 loadView: - (void)loadView { self.view = [[UIView al
如何始终如一地确定在 loadView 中布局 View 的正确大小? 我有不同的情况,其中 UIViewController 被推送到 UINavigationController,或者 UITab
我正在尝试设置一个 UITableViewController ,以便它的 tableView 属于我的自定义子类。我的 loadView 方法当前如下所示: - (void) loadView {
Xcode 4.3.3,ios 5.1,使用 ARC。我的第一个应用程序快完成了,有几个问题 1)我想知道不使用 Storyboard和不拖入我的 View 对象是否是不好的风格。如果我在 viewD
我正在实现 PFQueryTableView通过 Storyboard Controller 。我在实际实现代码时遵循了正确的方法(使用 initWithCoder: )。但是每当我尝试运行该应用程序
我正在尝试复制实现 In iOS, how do I create a button that is always on top of all other view controllers?在 Obj
只是想知道在 loadView 中你们是如何在 viewcontrollers 主视图中将 subview 居中的。我知道如何将它居中的代码,但问题是,在 loadView 中,viewcontrol
我创建了一个没有 xib 的 UITableViewController 子类的实例。我不会使用 xib 文件。我在 -(id)init 例程中构建数据。我在 -(id)init 函数中为表创建数据,
我是一名优秀的程序员,十分优秀!