Created
April 4, 2025 06:51
-
-
Save kopyl/2b17e2376962544a2b042c3b5a0a62c6 to your computer and use it in GitHub Desktop.
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 | |
| class FlippedView: NSView { | |
| override var isFlipped: Bool { | |
| return true | |
| } | |
| } | |
| private class Spacer: NSView { | |
| var height: CGFloat = 0 | |
| init(height: CGFloat) { | |
| super.init(frame: .zero) | |
| self.height = height | |
| translatesAutoresizingMaskIntoConstraints = false | |
| heightAnchor.constraint(equalToConstant: height).isActive = true | |
| } | |
| required init?(coder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| func add(on stackView: NSStackView) { | |
| let spacer = Spacer(height: self.height) | |
| stackView.addArrangedSubview(spacer) | |
| } | |
| } | |
| 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 stackViewPadding: CGFloat = 10 | |
| let spacer = Spacer(height: stackViewPadding) | |
| let scrollView = NSScrollView(frame: view.bounds) | |
| scrollView.hasVerticalScroller = true | |
| scrollView.autoresizingMask = [.width, .height] | |
| scrollView.translatesAutoresizingMaskIntoConstraints = false | |
| view.addSubview(scrollView) | |
| scrollView.drawsBackground = false | |
| let stackView = NSStackView() | |
| stackView.orientation = .vertical | |
| stackView.alignment = .leading | |
| stackView.spacing = 4 | |
| stackView.translatesAutoresizingMaskIntoConstraints = false | |
| let flippedView = FlippedView() | |
| flippedView.addSubview(stackView) | |
| scrollView.documentView = flippedView | |
| spacer.add(on: stackView) | |
| for item in items { | |
| let textView = NSTextField(labelWithString: item) | |
| stackView.addArrangedSubview(textView) | |
| } | |
| spacer.add(on: stackView) | |
| 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), | |
| ]) | |
| NSLayoutConstraint.activate([ | |
| stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: stackViewPadding), | |
| stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: stackViewPadding), | |
| ]) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment