Я просто перевел код из Сантьяго Gil Свифта 4.
let scrollView = NSScrollView(frame: CGRect(x: 0, y: 0, width: 400, height: 30))
var constraintViews: Dictionary<String, Any> = [:]
let documentView = NSView(frame: NSZeroRect)
var layoutFormat: String = "H:|-8-"
var size = NSSize(width: 8, height: 30)
for i in 0...8 {
let objectName: String = "button\(i)"
let button: NSButton = NSButton(title: objectName, target: nil, action: nil)
button.translatesAutoresizingMaskIntoConstraints = false
documentView.addSubview(button)
// Constraint Information
layoutFormat += "[\(objectName)]-8-"
constraintViews[objectName] = button
size.width += 8 + button.intrinsicContentSize.width
}
layoutFormat += "|"
let hConstraints: Array<NSLayoutConstraint> = NSLayoutConstraint.constraints(withVisualFormat: layoutFormat, options: .alignAllCenterY, metrics: nil, views: constraintViews)
documentView.frame = NSRect(x: 0, y: 0, width: size.width, height: size.height)
NSLayoutConstraint.activate(hConstraints)
scrollView.documentView = documentView
let item = NSCustomTouchBarItem(identifier: .controlScrubber)
item.view = scrollView
Надеюсь, это поможет кому-то ;-)