title: How to use custom NSView in Interface Builder?
body: <p>I have watched numerous tutorials on how to create a custom View in Interface Builder. All are for iOS but MacOS should be similar, no? I have tried a few methods but none are completely successful. The <code>init(coder:)</code> calls the NIB instantiation (either through <code>Bundle.main.loadNibNamed</code> or <code>NSNib</code> which, in turn, calls <code>init(coder:)</code> and ends up with infinite recursion if I class the main view in my nib as my custom class</p>
  
  <p>If I use a standard class then make file's owner my custom class that works better but still isn't right.</p>
  
  <p>Is there an example that creates a custom control, using AppKit, that works? The closest that I have come displays the custom control but none of the autolayout settings work.</p>
  
  <p>It must be fairly simple but I haven't figured it out yet.</p>
  
  <p>Here is what I have so far:</p>
  
  <ol>
  <li>A new class MyControl</li>
  </ol>
  
  <p><pre><code>
  import Cocoa</p>
  
  <p>@IBDesignable
  class MyControl: NSView {</p>
  
  <pre><code>@IBOutlet var customView: NSView!  // The top level NSView
  @IBOutlet weak var insideButton: NSButton!  // The button inside the view
  
  let myName: String
  
  required init?(coder: NSCoder) {
      super.init(coder: coder)
  
      if Bundle.main.loadNibNamed("MyControl", owner: self, topLevelObjects: nil) {
          addSubview(customView)
      }
  }
  </code></pre>
  
  <p>}
  </code></Pre></p>
  
  <ol start="2">
  <li><p>A nib based on NSView with contains a centered NSButton. The <code>File's Owner</code> class is set to <code>MyControl</code>, the top level view remains as <code>NSView</code></p></li>
  <li><p>The <code>Main.storyboard</code> has a Custom View classed as <code>MyControl</code> centered with height and width set.</p></li>
  </ol>
  
  <p>When I view <code>Main.storyboard</code> it has a frame for the custom view but it is blank.</p>
  
  <p>When I run the application the window that displays is blank.</p>