I created a simple project where an NSScrollView contains a vertical NSStackView with several colored square NSView items.
However, when I build and run the app, it launches already scrolled down by about two squares instead of showing the top square at the top of the window.
The expected behavior is that, when the app launches, the top square should be visible at the top of the scroll view.
What could be causing this issue, and how can I ensure the scroll view starts scrolled to the top?
Here is my code:
import Cocoa
class ViewController: NSViewController {
let scrollView = NSScrollView()
let stackView = NSStackView()
override func viewDidLoad() {
super.viewDidLoad()
// Configure stack view
stackView.orientation = .vertical
stackView.spacing = 20
stackView.alignment = .centerX
stackView.translatesAutoresizingMaskIntoConstraints = false
// Configure scroll view
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.documentView = stackView
scrollView.hasHorizontalScroller = false
scrollView.hasVerticalScroller = true
view.addSubview(scrollView)
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),
stackView.leadingAnchor.constraint(equalTo: scrollView.contentView.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: scrollView.contentView.trailingAnchor),
stackView.topAnchor.constraint(equalTo: scrollView.contentView.topAnchor),
// stackView.bottomAnchor.constraint(equalTo: scrollView.contentView.bottomAnchor),
// stackView.widthAnchor.constraint(equalTo: scrollView.contentView.widthAnchor),
])
let squareViews = prepareViews()
for squareView in squareViews {
stackView.addArrangedSubview(squareView)
}
}
private func prepareViews() -> [NSView] {
var views: [NSView] = []
let colors: [NSColor] = [.red, .green, .blue, .yellow, .systemPurple, .orange]
for _ in 0..<150 {
let index: Int = Int.random(in: 0..
Upadate: A previously asked question does not fully address my situation for several reasons:
- The original question was asked in the context of Objective-C, while I am working in Swift.
- That question focuses on how to set a default scroll position. In my case, I’m not even sure the issue is the lack of an explicitly defined default position; I would expect the default to be the top. It would be helpful if someone could clarify whether this assumption is correct and explain how AppKit determines a default position when none is specified.
- A comment in that thread provides a Swift snippet, but it lacks enough context for someone new to AppKit to understand where and how to apply it. For instance, the snippet refers to two scroll views (
scrollViewandleftScrollView0), whereas I only have one. It also calls a method namedreflectScrolledClipView, but attempting to use it in my setup results in a “Not found in scope” error.
For these reasons, I believe my question adds value for other new AppKit users working in Swift who may encounter the same issue.