如何在初始化过程中尽早获取子视图帧 [英] How to get subviews frames early in initialization process

查看:24
本文介绍了如何在初始化过程中尽早获取子视图帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些代码从 XIB 更改为故事板.设置故事板场景子视图的帧边界的时间似乎有很大差异.

I am changing some code from XIBs to storyboard. There seems to be a substantial difference in the timing of setting frame bounds of the subviews of a storyboard scene.

这段代码经常使用的技术是:

A technique this code frequently uses is this:

  • UIView 子视图具有在 XIB 或故事板文件中定义的维度(允许程序员以外的其他人修改 UI)
  • UIViewController 有一个链接到对象作为 IBObject UIView *
  • 在 UIViewController 的 - viewWillAppear() 中,进行了某些计算,并根据这些计算完成绘图.
  • a UIView subview has dimensions defined in the XIB or storyboard file (allowing somebody other than the programmer to modify the UI)
  • The UIViewController has a link to the object as an IBObject UIView *
  • in the UIViewController's - viewWillAppear(), certain calculations are made and drawing is done based upon those calculations.

例如,在我们的 XIB 或 Storyboard 文件中,有一个 UIView,我们希望将其切成四个区域.控制器查看 UIView 的边界,查看其框架,然后在 UIView 中绘制四个框,每个框的大小为四分之一.

For example, in our XIB or Storyboard file, there's a UIView that we wish to cut up into four regions. The controller looks at the bounds of that UIView, looks at its frame, and then in the UIView draws four boxes, each one quarter the size.

在 .h 中:

IBOutlet UIView *outputColorKeyView;

在 .m 的 viewWillAppear 中:

CGRect keyBounds = [outputColorKeyView bounds];

float high = keyBounds.size.height;
float wide = keyBounds.size.width;

float cellWidth = wide/4.0;

XLog(@"Dimensions for the color key are: %f %f", high, wide);

我的问题是,在 XIB 下,这工作正常:我能够看到我必须绘制的区域的尺寸为 200 点宽(例如),并且可以绘制 50 点宽的单元格.

My problem is that under XIBs, this worked fine: I was able to see that the dimensions of the region I had to draw in was 200 points wide (for example), and the cells could be drawn 50 points wide.

在 Storyboards 下,这不再适合我:当我从控制器的 viewWillAppear 方法调用时,我得到一个 (0,0,0,0) 的帧:

Under Storyboards, this is no longer working for me: I get a frame of (0,0,0,0) when I am calling from the controller's viewWillAppear method:

[18:09:16.901|4096] 颜色键的尺寸为:0.000000 0.000000

但是,从程序生命周期的后期运行(由按钮触发),我得到了正确的结果:

However, run from later in the program's life cycle (triggered by a button), I get the correct result:

[18:09:35.812|4096] 颜色键的尺寸为:36.000000 734.000000

有没有办法在视图出现之前获取这些信息?我会这么认为,但 viewWillAppear 似乎是实际呈现视图之前的最后机会.还是我遗漏了一些明显的开关,可以使这项技术按预期工作?

Is there any way to get this information before the view appears? I would think so, but viewWillAppear seems like the last opportunity before views are actually rendered. Or is there some obvious switch that I am missing that will make this technique work as intended?

谢谢

推荐答案

答案在我的后续问题 ...做这个东西的地方是在布局完成之后,在viewDidLayoutSubviews

The answer was given in an answer to my followup question ... the place to do this stuff is after the layout is done, in viewDidLayoutSubviews

我不清楚为什么信息发生了变化,从最初的描述文件(无论是 XIB 还是 Storyboard)中的数据早在 viewDidLoad 可用到现在的 (0,0,0,0),但是如果您遵循书籍和网络上的大量教程代码,则需要注意的不同.

It is not clear to me why the information changed, from the data in the initial description file (whether XIB or Storyboard) being available as early as viewDidLoad to now being (0,0,0,0), but it is a difference to be aware of if you follow a lot of the tutorial code out there in books and on the net.

这篇关于如何在初始化过程中尽早获取子视图帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆