7

我有一个配置 UITableView 可以通过 UINavigationController 方法启动颜色选择器:

[self.navigationController pushViewController:colorPickerViewController animated:YES];
[colorPickerViewController release];

这意味着 ColourPicker 将在顶部有一个导航栏(和后退按钮)

ColourPickerViewControll 及其视图 ColourPickerView 的结构如下:

- ColourPickerViewController - in it's XIB file at the top level view it has:
  - ColorPickerView : UIView (i.e. custom UI view) - in it's methods it has:
    - (id)initWithCoder:(NSCoder*)coder  {
       if ((self = [super initWithCoder:coder])) {
         CGFloat currVertBounds = self.bounds.size.height;

这里的问题是 currVertBounds 的值即将达到 480,所以它没有考虑导航栏

问题:如何获得 ColorPickerView 实例的真实显示高度?

是否与尝试在自定义 UIView 中计算布局有关,并且可能在那个阶段自定义视图未在整体 controll/navigationController 中呈现?

4

3 回答 3

7

你读bounds得太早了。发生的任何布局/子视图魔术都UIKit不会在[super initWithCoder:]. 您应该阅读bounds并展示您的观点:

  1. viewDidLoad/viewWillAppear中,为所有手动创建的 UI 元素设置自动调整大小参数,以便它们可以相对于不同的界面方向移动。在这些函数中,我不会依赖于bounds完全正确,但我会说它至少是正确的高度:宽度比。但是,它可能处于错误的方向。
  2. viewDidAppear中,如果您希望手动定位元素。当你viewDidAppear完成所有的调整大小等操作时,唯一需要担心的是未来的旋转,你应该调整你的视图 in willAnimateRotationToInterfaceOrientation:duration:

willAnimateRotationToInterfaceOrientation:duration:关于状态的文档:

调用此方法时,interfaceOrientation 属性已设置为新方向。因此,您可以在此方法中执行视图所需的任何其他布局。

iOS 5.0 文档添加了以下条款:

...设置为新的方向,并且视图的边界已更改。因此...

我认为以前的 iOS 版本仍然是这种情况。

于 2011-08-03T07:32:20.980 回答
1

在 Interface Builder 中创建时,iPhone 的视图定义为 480 像素的高度,所以这就是您在初始化视图时看到的。一旦视图被正确初始化并添加到视图堆栈中,它将调整大小以匹配实际可用空间。

您只是在调整之前询问其高度的视图。尝试读取 中的高度viewDidLoad,那么它应该是正确的。

于 2011-08-03T07:11:58.193 回答
0

是的,这是正确的,因为 UINavigationController 的实例是在您的 (delegate.h) 中声明的,并且 navigationBar 被添加到窗口而不是您的self.view

当您检查当前视图的边界时,它肯定会返回 320 * 480。它不应该在您的视图中包含导航栏的高度,因为它不在该视图中。它在窗户上。

于 2011-08-03T06:44:05.913 回答