Created
November 27, 2018 09:45
-
-
Save helje5/0456aafe2a27b4ed37ce08bb7a53f133 to your computer and use it in GitHub Desktop.
Nested NSStackViews lead to ambiguous layout
This file contains hidden or 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
import Cocoa | |
final class TestView : NSView { | |
static func makeAndAttachToView(_ rootView: NSView) { | |
let view = TestView() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
rootView.addSubview(view) | |
NSLayoutConstraint.activate([ | |
view.leadingAnchor .constraint(equalTo: rootView.leadingAnchor), | |
view.trailingAnchor.constraint(equalTo: rootView.trailingAnchor), | |
view.topAnchor .constraint(equalTo: rootView.topAnchor), | |
view.bottomAnchor .constraint(equalTo: rootView.bottomAnchor) | |
]) | |
} | |
override init(frame: NSRect) { | |
super.init(frame: frame) | |
NSLayoutConstraint.activate([ | |
widthAnchor .constraint(equalToConstant: 320), | |
heightAnchor.constraint(equalToConstant: 200) | |
]) | |
func makeStackView(_ orientation : NSUserInterfaceLayoutOrientation, | |
_ alignment : NSLayoutConstraint.Attribute, | |
_ view : NSView) | |
-> NSStackView | |
{ | |
// The subclasses are just for debugging in the Xcode View Debugger | |
let v = NSStackView(views: [view]) | |
v.translatesAutoresizingMaskIntoConstraints = false | |
v.orientation = orientation | |
v.alignment = alignment | |
v.spacing = 2.0 | |
return v | |
} | |
let searchField = NSSearchField(frame: NSMakeRect(0, 0, 128, 128)) | |
#if false // not ambiguous | |
let view = makeStackView(.horizontal, .centerY, searchField) | |
#else // ambiguous | |
let view = makeStackView(.horizontal, .centerY, | |
makeStackView(.vertical, .width, // .leading | |
searchField) | |
) | |
#endif | |
addSubview(view) | |
NSLayoutConstraint.activate([ | |
view.leadingAnchor .constraint(equalTo: self.leadingAnchor), | |
view.trailingAnchor.constraint(equalTo: self.trailingAnchor), | |
view.topAnchor .constraint(equalTo: self.topAnchor), | |
view.bottomAnchor .constraint(equalTo: self.bottomAnchor) | |
]) | |
} | |
required init?(coder decoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This removes the ambiguity:
Note: it really is the
NSStackView
specificsetHuggingPriority(_:for:)
, notsetContentHuggingPriority(_:for:)
, which still results in the ambiguity.