Apple 的文档说:
Note: Because the amount of space available for the custom view can vary
(depending on the size of the other navigation views), your custom view’s
autoresizingMask property should be set to have a flexible width and height.
Before displaying your view, the navigation controller automatically
positions and sizes it to fit the available space.
这很好,除了......我需要知道 UINavController 将使用的大小 BEFORE 将 View 添加到框架,因为我将显示的内容因大小而异。
这是在添加到 UINavigationController 的 UIViewController 的“loadView”方法中。
最佳答案
好吧,从技术上讲,您不应该在 loadView
方法中调整 subview 的大小。我不使用 nib,并且在我几乎所有的 View Controller 中,loadView
都非常小:self.view = [[UIView alloc] init];
。然后,在 viewDidLoad
中,我将所有 subview 添加到 self.view
。最后,我在 viewWillAppear
中调整了所有大小,因为此时 self.view
将是合适的大小。
如果您必须在 loadView
方法中调整 View 大小,则必须手动计算尺寸...即:找出设备的方向(self.interfaceOrientation
) 确定屏幕大小,减去您认为屏幕上的任何其他元素(如导航栏等)。但是不推荐这种方法。
关于ios - 在 loadView 期间或之前获取 UINavigationController 的内部 contentView 的 CGRect 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9605824/