我有一个配置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中呈现?
答案 0 :(得分:7)
您过早阅读bounds
。 UIKit
中发生的布局/子视图魔术都不会在[super initWithCoder:]
期间发生。您应该阅读bounds
并列出您的观点:
viewDidLoad
/ viewWillAppear
中,为所有手动创建的UI元素设置自动调整大小参数,以便它们可以针对不同的界面方向进行移动。在这些函数中,我不会依赖bounds
完全正确但我会说它至少是正确的高度:宽度比。但是,它可能是错误的方向。viewDidAppear
中,如果您希望手动定位元素。当你点击所有调整大小时viewDidAppear
已经发生,并且唯一需要担心的是未来的轮换,你应该在willAnimateRotationToInterfaceOrientation:duration:
调整你的视图。 willAnimateRotationToInterfaceOrientation:duration:
州的文档:
调用此方法时,interfaceOrientation属性已设置为新方向。因此,您可以在此方法中执行视图所需的任何其他布局。
iOS 5.0 docs添加子句:
...设置为新方向,并且视图的边界已更改。因此...
我认为以前的iOS版本仍然如此。
答案 1 :(得分:1)
根据定义,iPhone的视图在Interface Builder中创建时的高度为480像素,这就是您在初始化视图时所看到的内容。视图已正确初始化并添加到视图堆栈后,将调整其大小以匹配可用的实际空间。
你只是在调整之前询问它的高度。
尝试在viewDidLoad
中读取高度,然后它应该是正确的。
答案 2 :(得分:0)
是的,这是正确的,因为UINavigationController的实例是在你的(delegate.h)&中声明的。导航栏已添加到窗口,而不是 self.view
当您检查当前视图的边界时,它肯定会返回320 * 480。 它不应该包含您视图中导航栏的高度,因为它不在该视图上。它在窗口。