8

How can I disable the vertical scrolling of a NSScrollView ?

I can't find a quick solution on Google.

Thanks

aneuryzm
  • 63,052
  • 100
  • 273
  • 488
  • Possible duplicated: http://stackoverflow.com/questions/12626288/disable-scrolling-in-nstableview – gwdp Mar 01 '13 at 01:54

4 Answers4

9

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]);
Ivan Androsenko
  • 608
  • 6
  • 15
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.

Brad Hesse
  • 648
  • 6
  • 15
-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?

dasdom
  • 13,975
  • 2
  • 47
  • 58
-9

was wondering if you came across

- (void)setHasVerticalScroller:(BOOL)flag;

to disable the vertical scrollbar ?

You can also set this in Interface Builder.

jankoen
  • 518
  • 5
  • 10