为什么self.bounds.size.height没有考虑此代码中的UINavigationView导航栏?

Gre*_*reg 7 iphone uiview uinavigationcontroller bounds ios

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

[self.navigationController pushViewController:colorPickerViewController animated:YES];
[colorPickerViewController release];
Run Code Online (Sandbox Code Playgroud)

这意味着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;
Run Code Online (Sandbox Code Playgroud)

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

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

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

dar*_*s0n 7

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

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

willAnimateRotationToInterfaceOrientation:duration:国家文件:

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

iOS 5.0 docs添加了以下子句:

...设置为新方向,并且视图的边界已更改.从而...

我认为以前的iOS版本仍然如此.