Skip to content

Instantly share code, notes, and snippets.

@kopyl
Created April 4, 2025 06:16
Show Gist options
  • Save kopyl/81f42880fe53cc22c1cc72f8cef40d91 to your computer and use it in GitHub Desktop.
Save kopyl/81f42880fe53cc22c1cc72f8cef40d91 to your computer and use it in GitHub Desktop.
import Cocoa
class FlippedView: NSView {
override var isFlipped: Bool {
return true
}
}
class ViewController: NSViewController {
let items = Array(0..<50).map { String($0) }
override func loadView() {
self.view = NSView(frame: NSRect(x: 0, y: 0, width: 400, height: 300))
}
override func viewDidLoad() {
super.viewDidLoad()
let scrollView = NSScrollView(frame: view.bounds)
scrollView.hasVerticalScroller = true
scrollView.autoresizingMask = [.width, .height]
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
let stackView = NSStackView()
stackView.orientation = .vertical
stackView.alignment = .leading
stackView.spacing = 4
stackView.translatesAutoresizingMaskIntoConstraints = false
let flippedView = FlippedView()
flippedView.addSubview(stackView)
scrollView.documentView = flippedView
for item in items {
let textView = NSTextField(labelWithString: item)
stackView.addArrangedSubview(textView)
}
let height = stackView.fittingSize.height
scrollView.documentView?.frame.size = CGSize(width: scrollView.contentSize.width, height: height)
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment