How can I disable horizontal scrolling for an NSScrollView?
Asked Answered
A

3

11

I first tried disabled the scroll bar by setting:

scrollView!.hasHorizontalScroller = false

and that successfully worked, but I could still scroll from left to right by using my trackpad. Is there a way to ensure that horizontal scrolling is completely disabled for an NSScrollView object?

Agger answered 26/10, 2014 at 20:14 Comment(0)
D
-5
var scrollView = UIScrollView(frame:myFrame)
let scroll = CGSizeMake(myHeight, myFrame.size.width)
scrollView.contentSize = scroll

Fixes the frame width to prevent horizontal scrolling.

Durant answered 26/10, 2014 at 20:26 Comment(3)
A similar approach worked. However, setting the contentSize of the NSScrollView was not permitted (it was a read only variable), but the general concept of your solution was valid. Thanks!Agger
UIScrollView ≠ NSScrollViewCitation
Question is asking about NSScrollView.Knowledgeable
U
7

Even with a correct content size, I was still able to use two-finger gestures to scroll horizontally a bit before it snaps back into place. To fix this, you can set the elasticity:

scrollView.horizontalScrollElasticity = .None
Usurpation answered 15/3, 2016 at 19:24 Comment(0)
I
0

In addition to @mikepj's answer I also needed to do the following. Modifying this to become:

override func scrollWheel(with event: NSEvent) {
    super.scrollWheel(with: event)
    
    if event.deltaX != 0 {
        super.nextResponder?.scrollWheel(with: event)
    }
}

This may be because I was using an NSRulerView.

Inclusion answered 29/1, 2022 at 5:11 Comment(0)
D
-5
var scrollView = UIScrollView(frame:myFrame)
let scroll = CGSizeMake(myHeight, myFrame.size.width)
scrollView.contentSize = scroll

Fixes the frame width to prevent horizontal scrolling.

Durant answered 26/10, 2014 at 20:26 Comment(3)
A similar approach worked. However, setting the contentSize of the NSScrollView was not permitted (it was a read only variable), but the general concept of your solution was valid. Thanks!Agger
UIScrollView ≠ NSScrollViewCitation
Question is asking about NSScrollView.Knowledgeable

© 2022 - 2024 — McMap. All rights reserved.