8

How can I disable the vertical scrolling of a NSScrollView ?

I can't find a quick solution on Google.

Thanks

1

4 Answers 4

10

Try this in NSScrollView subclass:

- (void)scrollWheel:(NSEvent *)theEvent
{
    [super scrollWheel:theEvent];

    if ([theEvent deltaY] != 0.0)
    {
        [[self nextResponder] scrollWheel:theEvent];
    }
}

Also you can log it by:

NSLog(@"user scrolled %f horizontally and %f vertically", [theEvent deltaX], [theEvent deltaY]);
1
  • This does not block automatic scrolling when reseting the bounds for example. May 27, 2021 at 17:42
2

I like Ivan's answer above. However, in my particular case, I had a table view (vertical scrolling) where each row contained a collection view (horizontal scrolling).

When users tried to scroll up/down, if there mouse was hovered over one of the collection views, they wouldn't be able to scroll up or down.

To resolve this, I subclassed the NSScrollView that contained the NSCollectionViews and I overrided this method with the following code:

override func scrollWheel(with event: NSEvent)
{
    if (fabs(Double(event.deltaY)) > fabs(Double(event.deltaX))) {
        self.nextResponder?.scrollWheel(with: event);
    } else {
        super.scrollWheel(with: event);
    }
}

This way, it checks if the user is predominantly scrolling in one direction and handles it appropriately.

-1

You could observe the content offset in scrollViewDidScroll: and set the x value to the value you like.

Edit: Ok, then. Have you seen this: Constraining Scrolling?

4
  • 2
    I'm on mac, the link you gave me is for iOS
    – aneuryzm
    Mar 21, 2012 at 8:53
  • I've checked the link you passed me. I don't understand why I get null bounds: [scrolledView bounds] is (null).
    – aneuryzm
    Mar 21, 2012 at 9:46
  • Ya, the scrolledView is a NSClipView (just checked in the console) which is a subclass of NSView, so it should have bounds
    – aneuryzm
    Mar 21, 2012 at 10:15
  • The link above to "Constraining Scrolling" contains the correct answer. Thanks! May 26, 2013 at 22:05
-9

was wondering if you came across

- (void)setHasVerticalScroller:(BOOL)flag;

to disable the vertical scrollbar ?

You can also set this in Interface Builder.

1
  • 6
    It hides the scroller, but the scrolling is still possible by gestures
    – aneuryzm
    Mar 22, 2012 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.