ios - iOS 在显示之前如何设置 ViewController View 框架?

标签 ios uiview uiviewcontroller

我不熟悉 iOS UIViewController 的详细实现。我有以下代码来创建一个新的 UIViewController 并显示它,但是我在 initWithFrame 方法中设置的框架不起作用, Controller 的 View 始终是全屏 (320*480)。

UIViewController *viewController = [[UIViewController alloc] init];

// view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 130)];
viewController.view = view;
[view release];
AppController *app = (AppController*)[[UIApplication sharedApplication] delegate];

UINavigationController *nav = [app navController];
[nav pushViewController:viewController animated:YES];
 [viewController release];

我在 apple developer documents 中搜索 releative thoughts,但我没有发现任何有用的东西。 UIViewController 在显示它之前如何处理它的 View 框架属性?我在哪里可以找到有用的文档。

谢谢。


更新:其实代码来自cocos2d-iphone DirectorTest: https://github.com/cocos2d/cocos2d-iphone/blob/release-2.0-rc1/tests/DirectorTest.m#L143

最佳答案

你完全错了 - 你真的需要阅读(并理解)Apple 的 UIViewController 文档:

View Controller Catalog

View Controller Programming Guide

View Controller Reference

如果您在 View Controller 的代码中创建 View ,您应该在 View Controller 的 loadView 方法中执行此操作。

直接来自 Apple 的文档:

Creating a View Programmatically

If you prefer to create views programmatically ... you do so by overriding your view controller’s loadView method. Your implementation of this method should do the following:

Create a root view object. The root view contains all other views associated with your view controller. You typically define the frame for this view to match the size of the app window, which itself should fill the screen. However, the frame is adjusted based on how your view controller is displayed. See “View Controller View Resizing.”

You can use a generic UIView object, a custom view you define, or any other view that can scale to fill the screen.

Create additional subviews and add them to the root view. For each view, you should do the following:

Create and initialize the view. For system views, you typically use the initWithFrame: method to specify the initial size and position of the view. Add the view to a parent view using the addSubview: method. Assign the root view to the view property of your view controller.

关于ios - iOS 在显示之前如何设置 ViewController View 框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10820037/

相关文章:

ios - UIView原点不等于0

ios - 在其自身的功能中无法推送 View Controller

ios - 如何从 Pan 手势触发 Segue?

c++ - 编译外部 C++ 库以用于 iOS 项目

ios - 使用 plist 在 TableView 之间传递数据

ios - 将可达性与 ReactiveCocoa 集成?

iphone - 在这种情况下我应该使用全局 bool 变量吗?

ios - -[EAGLContext renderbufferStorage :fromDrawable:] Failing the second time on?

ios - 在两个UIFonts之间进行插值

ios - 为 View 设置标签