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

发布于 2024-11-27 15:30:59 字数 855 浏览 1 评论 0原文

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

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

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

的结构如下:

- 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;

ColourPickerViewControll 及其视图ColourPickerView 这里的问题是 currVertBounds 的值即将达到 480,因此它没有考虑导航栏

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

这是否与尝试在自定义 UIView 中计算布局有关,并且也许自定义视图在该阶段没有在整个 controll/navigationController 中呈现?

I have a configuration UITableView that can launch a colour picker via a UINavigationController approach:

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

The effect of this means the ColourPicker will have a navigation bar at the top ( and back button)

The structure of the ColourPickerViewControll and it view ColourPickerView is as follows:

- 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;

The issue here is the the value of currVertBounds is coming to 480, so it's not taking account of the navigation bar

QUESTION: How do I get the true displayed height of the ColorPickerView instance?

Is it something to do with trying to get the layout calculated in the custom UIView, and perhaps the custom view isn't rendered within the overall controll/navigationController at that stage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人生戏 2024-12-04 15:30:59

您过早地阅读了boundsUIKit 中发生的布局/子视图魔法不会在 [super initWithCoder:] 期间发生。您应该阅读 bounds 并布局您的视图:

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

文档 willAnimateRotationToInterfaceOrientation:duration: 状态:

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

iOS 5.0 文档添加了子句:

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

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

You're reading bounds too early. None of the layout/subviewing magic that happens in UIKit will take place during [super initWithCoder:]. You should be reading bounds and laying out your view either:

  1. In viewDidLoad/viewWillAppear, and set up the autosizing parameters for all manually created UI elements so they can get moved around with respect to different interface orientations. In these functions I wouldn't rely on bounds being exactly correct but I would say it's at least the right height:width ratio. It could possibly be in the wrong orientation, though.
  2. In viewDidAppear, if you wish to manually position elements. By the time you hit viewDidAppear all of the resizing and such has taken place, and the only thing left to worry about is future rotations, which you should adjust your view for in willAnimateRotationToInterfaceOrientation:duration:.

Docs on willAnimateRotationToInterfaceOrientation:duration: state:

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 docs add the clause:

...set to the new orientation , and the bounds of the view have been changed. Thus...

I think this is still the case for prior iOS versions.

梦里南柯 2024-12-04 15:30:59

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

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

Views for iPhone will by definition have a height of 480 pixel when created in Interface Builder, so that's what you're seeing when you're initializing the view. Once the view has been properly initialized and added to the view stack, it will be resized to match the actual space available.

You're simply asking the view of its height before it has been adjusted.
Try reading the height in viewDidLoad, then it should be correct.

对风讲故事 2024-12-04 15:30:59

是的,这是正确的,因为 UINavigationController 的实例是在您的 (delegate.h) & 中声明的。导航栏添加到 window 而不是您的 self.view

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

Yes, this is right because the instance of UINavigationController is declared in your (delegate.h) & that navigationBar is added to window not on your self.view

When you check the bounds of your current view, it should definitely return 320 * 480.
It should not include height of navigation bar in you view, as it not on that view. Its on window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文