1
votes

I try to create a scrollview. Created a scrollview via Xib, programmatically added scrolledView (a NSView that contains all subview in scrollView).

The following code shows the subviews but the scrollView not scroll. Why?

class LevelScrollController: NSViewController {

@IBOutlet var scrollView: NSScrollView!

override func viewDidLoad() {
    super.viewDidLoad()

    let scrolledView = NSView(frame: NSRect(x: 0, y: 0, width: scrollView.frame.size.width, height: 300))

    // Inserisco pulsanti di esempio
    for i in 1 ... 10{
        scrolledView.addSubview(NSButton(frame: NSRect(x: 0, y: i*30, width: 100, height: 30)))
    }
    scrollView.addSubview(scrolledView)
}

Putting the code in viewDidLayout instead of viewDidLoad not change the result: not scroll

1

1 Answers

6
votes

Instead of add scrolledView to subview of scrollView, set it as documentView.

Replace scrollView.addSubview(scrolledView) with scrollView.documentView = scrolledView