Hard to believe something this old doesn't work, but it appears so. I set the top
to 10 and the bottom
to 0, but it uses 10 for both.
Am I doing something wrong, or is it a bug?
sv.edgeInsets = .init(top: 10, left: 10, bottom: 0, right: 0)
Screenshot:
Full code example below.
(This is the ViewController.swift file after creating a new Xcode project with "Interface" of "Storyboard". No other changes to the project template.)
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let sv = NSStackView()
sv.translatesAutoresizingMaskIntoConstraints = false
let v1 = ColoredBlock(.red, width: 20)
let v2 = ColoredBlock(.green, width: 20)
let v3 = ColoredBlock(.blue)
sv.addArrangedSubview(v1)
sv.addArrangedSubview(v2)
sv.addArrangedSubview(v3)
sv.spacing = 0
sv.edgeInsets = .init(top: 10, left: 10, bottom: 0, right: 0)
// sv.setContentHuggingPriority(.required, for: .vertical)
view.addSubview(sv)
pinToFourEdges(view, sv)
}
}
class ColoredBlock: NSView {
let intrinsicHeight: CGFloat
let intrinsicWidth: CGFloat
init(_ color: NSColor, width: CGFloat = NSView.noIntrinsicMetric, height: CGFloat = NSView.noIntrinsicMetric) {
intrinsicWidth = width
intrinsicHeight = height
super.init(frame: .zero)
self.wantsLayer = true
if let layer = self.layer {
layer.backgroundColor = color.cgColor
}
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override var intrinsicContentSize: NSSize {
return NSSize(width: intrinsicWidth, height: intrinsicHeight)
}
}
public func pinToFourEdges(_ view1: NSView, _ view2: NSView) {
view1.topAnchor.constraint(equalTo: view2.topAnchor).isActive = true
view1.bottomAnchor.constraint(equalTo: view2.bottomAnchor).isActive = true
view1.leftAnchor.constraint(equalTo: view2.leftAnchor).isActive = true
view1.rightAnchor.constraint(equalTo: view2.rightAnchor).isActive = true
}