title: Dynamically resize NSWindow based on NSOutlineView height
body: <p>I have read many answers to questions about dynamically resizing NSWindows and nothing has quite worked yet.</p>
  
  <p>I have built a 'popover' like window to be displayed form the menu bar, I couldn't use NSPopover because it isn't quite customisable enough in terms of design. My view hierarchy current looks like this:</p>
  
  <pre><code>NSWindow Subclass
    - NSView (clears the titlebar rendering, draws popover arrow)
       - NSView (contentView)
          - NSOutlineView (main table of content)
          - NSView (window footer)
  </code></pre>
  
  <p>What I need is for the window to expand and contract with items in the NSOutlineView expanding and contracting, so that the window is always precisely the correct height for the outline view, footer, and the popover arrow at the top.</p>
  
  <p>I have methods to calculate the required height for the outline view based on the content. So far I have been trying to recalculate the size when <code>awakeFromNib</code> is called on the content view controller, and then again on the delegate methods <code>outlineViewItemDidCollapse:</code> and <code>outlineViewItemDidExpand:</code>, but I am never able to resize the window correctly.</p>
  
  <p>I have tried lots of different ways and none worked. Is there a 'canonical' way to do this? I've seen people talking about <code>-[NSWindow frameForContentRect:]</code> in relation to this sort of problem, but I can't quite work out how that is needed.</p>
  
  <p>Maybe I am going about this completely the wrong way, I hope not though. It seems like this should be possible, it's just getting the right things in the right places. Unfortunately GUI programming is not a strong point for me. I would appreciate any ideas or solutions people have.</p>
  
  <p>Thanks.</p>
  
  <p><strong>Edit:</strong> This was solved, partly due to the marked answer, and partly due to a few other things. The clearing NSView did not have an autoresize mask, so I set this to expand in all directions, this helped with some of the resizing issues. Also, the method I was using for calculating the height required was not entirely correct and had a few problems. </p>
  
  <p>With the provided answer, the way to find the required height is good for some circumstances, but I didn't have a 'root' item that could be measured. This is something that may need to be taken into account.</p>