【问题标题】:Why is self.bounds.size.height not taking into account the UINavigationView nav bar in this code?为什么 self.bounds.size.height 不考虑此代码中的 UINavigationView 导航栏?
【发布时间】:2011-08-03 06:23:23
【问题描述】:

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

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

这样的效果意味着颜色选择器顶部会有一个导航栏(和返回按钮)

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 中呈现?

【问题讨论】:

  • 你想改变导航栏或nt的高度。

标签: iphone ios uiview uinavigationcontroller bounds


【解决方案1】:

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

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

willAnimateRotationToInterfaceOrientation:duration: 状态的文档:

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

iOS 5.0 文档添加子句:

...设置为新的方向,并且视图的边界已更改。于是……

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2014-11-26
        • 2011-08-26
        • 2021-08-17
        • 2019-12-13
        • 2020-10-18
        • 1970-01-01
        • 1970-01-01
        • 2018-05-14
        • 1970-01-01
        相关资源
        最近更新 更多