Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get subviews frames early in initialization process
    text
    copied!<p>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.</p> <p>A technique this code frequently uses is this: </p> <ul> <li>a UIView subview has dimensions defined in the XIB or storyboard file (allowing somebody other than the programmer to modify the UI)</li> <li>The UIViewController has a link to the object as an <code>IBObject UIView *</code></li> <li>in the UIViewController's <code>- viewWillAppear()</code>, certain calculations are made and drawing is done based upon those calculations.</li> </ul> <p>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.</p> <p>In .h:</p> <pre><code>IBOutlet UIView *outputColorKeyView; </code></pre> <p>In .m's <code>viewWillAppear</code>:</p> <pre><code>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); </code></pre> <p>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.</p> <p>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 <code>viewWillAppear</code> method:</p> <p><code>[18:09:16.901|4096] Dimensions for the color key are: 0.000000 0.000000</code></p> <p>However, run from later in the program's life cycle (triggered by a button), I get the correct result:</p> <p><code>[18:09:35.812|4096] Dimensions for the color key are: 36.000000 734.000000</code></p> <p>Is there any way to get this information before the view appears? I would think so, but <code>viewWillAppear</code> 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?</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload