Last active
September 26, 2020 15:42
-
-
Save KevinGutowski/acdab32a6c470cc8bd0865947f12a652 to your computer and use it in GitHub Desktop.
Stack view example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let threadDictionary = NSThread.mainThread().threadDictionary() | |
let panelID = "com.testPanel" | |
if (threadDictionary[panelID]) { | |
let panel = threadDictionary[panelID] | |
panel.close() | |
threadDictionary.removeObjectForKey(panelID) | |
} | |
var panelWidth = 312 | |
var panelHeight = 210 | |
let panel = NSPanel.alloc().init() | |
panel.setFrame_display(NSMakeRect(0, 0, panelWidth, panelHeight), true) | |
panel.setStyleMask(NSTexturedBackgroundWindowMask | NSTitledWindowMask | NSClosableWindowMask) | |
panel.title = "Testing" | |
panel.center() | |
panel.makeKeyAndOrderFront(null) | |
panel.setLevel(NSFloatingWindowLevel) | |
panel.standardWindowButton(NSWindowMiniaturizeButton).setHidden(true) | |
panel.standardWindowButton(NSWindowZoomButton).setHidden(true) | |
let view1 = NSButton.alloc().initWithFrame(NSMakeRect(0,0,300,20)) | |
view1.setWantsLayer(true) | |
view1.layer().setBackgroundColor(NSColor.systemBlueColor().CGColor()) | |
view1.setButtonType(NSRadioButton) | |
let view2 = NSButton.alloc().initWithFrame(NSMakeRect(0,0,300,20)) | |
view2.setWantsLayer(true) | |
view2.layer().setBackgroundColor(NSColor.systemRedColor().CGColor()) | |
view2.setButtonType(NSRadioButton) | |
let view3 = NSButton.alloc().initWithFrame(NSMakeRect(0,0,300,20)) | |
view3.setWantsLayer(true) | |
view3.layer().setBackgroundColor(NSColor.systemGreenColor().CGColor()) | |
view3.setButtonType(NSRadioButton) | |
let stackView = NSStackView.stackViewWithViews([view1,view2,view3]) | |
stackView.setOrientation(NSUserInterfaceLayoutOrientationVertical) | |
stackView.setAlignment(NSLayoutAttributeLeading) | |
stackView.setSpacing(4) | |
console.log(stackView.frame()) | |
panel.contentView().addSubview(stackView) | |
let viewsDictionary = {"stackview" : stackView} | |
let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat_options_metrics_views("H:|-0-[stackview]-0-|", 0, nil, viewsDictionary); | |
let horizontalConstraints2 = NSLayoutConstraint.constraintsWithVisualFormat_options_metrics_views("H:|-10-[stackview]-0-|", 0, nil, viewsDictionary); | |
let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat_options_metrics_views("V:|-0-[stackview]-0-|", 0, nil, viewsDictionary); | |
panel.contentView().addConstraints(horizontalConstraints) | |
panel.contentView().addConstraints(verticalConstraints) | |
panel.contentView().addConstraints(horizontalConstraints2) | |
console.log(panel.contentView().subviews()[0].frame()) | |
NSUserDefaults.standardUserDefaults().setObject_forKey(true, "NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment