我有一个配置 UITableView,可以通过 UINavigationController 方法启动颜色选择器:
[self.navigationController pushViewController:colorPickerViewController animated:YES];
[colorPickerViewController release];
这意味着 ColourPicker 的顶部将有一个导航栏(和后退按钮)
ColourPickerViewControll及其 View 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 中计算布局有关,也许在那个阶段自定义 View 未在整体控件/导航 Controller 中呈现?
最佳答案
您阅读 bounds
太早了。在 [super initWithCoder:]
期间,UIKit
中发生的所有布局/ subview 魔法都不会发生。您应该阅读 bounds
并布置您的 View :
- 在
viewDidLoad
/viewWillAppear
中,为所有手动创建的 UI 元素设置自动调整大小参数,以便它们可以根据不同的界面方向移动。在这些函数中,我不会依赖于bounds
是完全正确的,但我会说它至少是正确的高宽比。不过,它可能方向错误。 - 在
viewDidAppear
中,如果您希望手动定位元素。当您点击viewDidAppear
时,所有调整大小等都已经发生,唯一需要担心的是 future 的旋转,您应该在willAnimateRotationToInterfaceOrientation:duration 中调整您的 View :
。
关于 willAnimateRotationToInterfaceOrientation:duration:
的文档状态:
By the time this method is called, the interfaceOrientation property is already set to the new orientation. Thus, you can perform any additional layout required by your views in this method.
iOS 5.0 文档添加条款:
...设置为新方向, View 的边界已更改。于是……
我认为以前的 iOS 版本仍然是这种情况。
关于iphone - 为什么 self.bounds.size.height 没有考虑这段代码中的 UINavigationView 导航栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6922461/