title: How to implement sizeToFit if it depends on layoutSubviews?
body: <p>In the documentation of <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/layoutSubviews" rel="nofollow">layoutSubviews</a>, Apple says:</p>
  
  <blockquote>
    <p>You should not call this method directly.</p>
  </blockquote>
  
  <p>I'm trying to implement <code>sizeToFit</code>. I want it to put a tight bounding box on all of the subviews. I have to layout the subviews before determining such a bounding box. That means I must call <code>layoutSubviews</code>, which Apple frowns upon. How would I solve this dilemma without violating Apple's rules?</p>
  
  <pre><code>- (void)layoutSubviews
  {
    [super layoutSubviews];
  
    self.view0.frame = something;
    self.view1.frame = somethingElse;
  }
  
  - (void)sizeToFit
  {
     [self layoutSubviews];
  
     self.frame = CGRectMake(
       self.frame.origin.x,
       self.frame.origin.y,
       MAX(
         self.view0.frame.origin.x + self.view0.frame.size.width,
         self.view1.frame.origin.x + self.view1.frame.size.width
       ),
       MAX(
         self.view0.frame.origin.y + self.view0.frame.size.height,
         self.view1.frame.origin.y + self.view1.frame.size.height
       )
    );
  }
  </code></pre>