Swift3.0 development of macOS applications (11): how to add an external view of the Responder First Storyboard

When using the Storyboard layout, to the content of their needs will appear more difficult with a view controller, often require additional to create Xib, then import the Xib file to create the view controller, and then loaded into the code, but this is trouble. We can not create new Xib, add new map for use when you want to use the controller. (macOS and iOS use this method)

Drag and drop the desired view (e.g. NSView) to the controller First Responder
Swift3.0 development of macOS applications (11): how to add an external view of the Responder First Storyboard
QQ20170321-133643.png
There will be a fixed position of view, can only adjust the size and add controls to the view, the location can not move. You can see from the tree structure that it is at the same level as ViewController.

Views of this hierarchy can be properly connected to IBAction and IBOutlet, eliminating the trouble of re creating the value of xib.

Swift3.0 development of macOS applications (11): how to add an external view of the Responder First Storyboard
QQ20170321-134411.png

After connecting the end of the line, do the following example.

Swift3.0 development of macOS applications (11): how to add an external view of the Responder First Storyboard
18.gif

The code is as follows:

Var isShow = Bool (@IBOutlet) var redView: override func viewDidLoad (NSView!) (@IBAction func) {super.viewDidLoad} showOrHide (_ sender: Any) {isShow = isShow! If isShow view.addSubview (redView) {redView.wantsLayer = true / redViewAddConstraint (redView) to add constraint}else{(redView.removeFromSuperview)}} {redView.layer (func redViewAddConstraint).BackgroundColor? RedView.translatesAutoresizingMaskIntoConstraints = NSColor.red.cgColor = false (redView.centerXAnchor.constraint equalTo: view.centerXAnchor.IsActive true redView.centerYAnchor.constraint (equalTo:) = view.centerYAn Chor).IsActive = true redView.widthAnchor.constraint (equalToConstant: 100).IsActive = true redView.heightAnchor.constraint (equalToConstant: 100).IsActive = true}

Of course, you can add any controls, not just NSView.

Source code – GitHub