curiously77
curiously77

Reputation: 233

Resizing NSScrollView

How can I resize NSScrollView using Swift?

class MainViewController: NSViewController {

    @IBOutlet weak var scrollView: NSScrollView!

    override func viewDidLoad {
        scrollView.setFrameOrigin(NSPoint(x: 340, y: 537))
        scrollView.setFrameSize(CGSize(width: scrollView.contentSize.width, height: 102.0))
    }

}

The above code works only if there are no constraints set. But, without constraints, the ScrollView doesn't stay in place when the window is resized.

Upvotes: 0

Views: 833

Answers (2)

curiously77
curiously77

Reputation: 233

So this worked for me since my ScrollView was positioned on the top left:

scrollView.setFrameOrigin(NSPoint(x: self.view.frame.minX + 340, y: self.view.frame.maxY - 172))
scrollView.setFrameSize(CGSize(width: scrollView.contentSize.width, height: 102.0))

And the below image shows the settings used in Size Inspector to enable auto resize and also to stay to the top-left of the window.

ScrollView Size Inspector

Upvotes: 1

Chris Zielinski
Chris Zielinski

Reputation: 1391

If you are trying to set the document size (scrollable area) of a NSScrollView, then all we need to do is set the document view's size.

📌 Note: This assumes there are no constraints on the document view.

scrollView.documentView?.setFrameSize(NSSize(width: 576, height: 56))

If you are trying to scroll to a specific region, see scrollToVisible(_:).

Upvotes: 1

Related Questions