How to get subviews frames early in initialization process

谁说胖子不能爱 提交于 2019-12-11 02:12:01

问题


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:

  • 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.

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.

In .h:

IBOutlet UIView *outputColorKeyView;

In .m's 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);

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.

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] Dimensions for the color key are: 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] Dimensions for the color key are: 36.000000 734.000000

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?

Thanks


回答1:


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

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.



来源:https://stackoverflow.com/questions/13389750/how-to-get-subviews-frames-early-in-initialization-process

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!