Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Presumably you're calling the demo code when the view controller first loads its view, like in <code>viewDidLoad</code> or another life cycle method. The nested subview's geometries won't reflect its constraints until <code>viewDidLayoutSubviews</code> is called. Nothing you do during the initial life cycle of a view controller will make that method arrive any faster. </p> <p><strong>Update 12/30/13:</strong> After testing Aaron Brager's sample code, I now realize that the previous paragraph is incorrect. Apparently, you can force layout in <code>viewDidLoad</code> by calling <code>setNeedsLayout</code> followed by <code>layoutIfNeeded</code>. </p> <p>If you executed the demo code in response to a button click instead, I think you'll see the final geometries of your nested subview logged <em>before</em> the action method completes. </p> <pre><code>- (IBAction)buttonTapped:(id)sender { UIView *view = [[UINib nibWithNibName:@"View" bundle:nil] instantiateWithOwner:nil options:nil][0]; view.frame = CGRectMake(0, 0, 200, 200); [self.view addSubview:view]; [self.view layoutIfNeeded]; NSLog(@"Subviews: %@", view.subviews); } </code></pre> <p>In the latter case, you can request layout on-demand because the view controller has completed its initial setup.</p> <p>But during a view controller's initial setup, how are you going to get the final geometries of your re-usable subview? </p> <p>After you set the content for the re-usable subview, have your view controller ask the subview for its size. In other words, implement a method on your custom view that calculates the size based on the content.</p> <p>For example, if the subview's content is an attributed string, you could use a method like <code>boundingRectWithSize:options:context:</code> to help determine the size of your subview. </p> <pre><code>CGRect rect = [attributedString boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsersLineFragmentOrigin context:nil]; </code></pre>
 

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