Disable scrolling in NSTableView

2019-02-17 03:30发布

Is there a simple way to disable scrolling of an NSTableView.

It seems there isn't any property on [myTableView enclosingScrollView] or [[myTableView enclosingScrollView] contentView] to disable it.

5条回答
混吃等死
2楼-- · 2019-02-17 03:51

Works for me:

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

    if ([theEvent deltaY] != 0)
    {
        [[self nextResponder] scrollWheel:theEvent];
    }
}
查看更多
仙女界的扛把子
3楼-- · 2019-02-17 03:56

There is no simple direct way (meaning, there's no property like UITableView's scrollEnabled that you can set), but i found this answer helpful in the past.

One other thing you could try (not sure about this) is subclassing NSTableView and override -scrollWheel and -swipeWithEvent so they do nothing. Hope this helps

查看更多
狗以群分
4楼-- · 2019-02-17 04:02

This works for me: subclass NSScrollView, setup and override via:

- (id)initWithFrame:(NSRect)frameRect; // in case you generate the scroll view manually
- (void)awakeFromNib; // in case you generate the scroll view via IB
- (void)hideScrollers; // programmatically hide the scrollers, so it works all the time
- (void)scrollWheel:(NSEvent *)theEvent; // disable scrolling

@interface MyScrollView : NSScrollView
@end

#import "MyScrollView.h"

@implementation MyScrollView

- (id)initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];
    if (self) {
        [self hideScrollers];
    }

    return self;
}

- (void)awakeFromNib
{
    [self hideScrollers];
}

- (void)hideScrollers
{
    // Hide the scrollers. You may want to do this if you're syncing the scrolling
    // this NSScrollView with another one.
    [self setHasHorizontalScroller:NO];
    [self setHasVerticalScroller:NO];
}

- (void)scrollWheel:(NSEvent *)theEvent
{
    // Do nothing: disable scrolling altogether
}

@end

I hope this helps.

查看更多
成全新的幸福
5楼-- · 2019-02-17 04:04

Here's the best solution in my opinion:

Swift 4

import Cocoa

@IBDesignable
@objc(BCLDisablableScrollView)
public class DisablableScrollView: NSScrollView {
    @IBInspectable
    @objc(enabled)
    public var isEnabled: Bool = true

    public override func scrollWheel(with event: NSEvent) {
        if isEnabled {
            super.scrollWheel(with: event)
        }
        else {
            nextResponder?.scrollWheel(with: event)
        }
    }
}


Simply replace any NSScrollView with DisablableScrollView (or BCLDisablableScrollView if you still use ObjC) and you're done. Simply set isEnabled in code or in IB and it will work as expected.

The main advantage that this has is for nested scroll views; disabling children without sending the event to the next responder will also effectively disable parents while the cursor is over the disabled child.

Here are all advantages of this approach listed out:

  • ✅ Disables scrolling
    • ✅ Does so programmatically, behaving normally by default
  • ✅ Does not interrupt scrolling a parent view
  • ✅ Interface Builder integration
  • ✅ Drop-in replacement for NSScrollView
  • ✅ Swift and Objective-C Compatible
查看更多
乱世女痞
6楼-- · 2019-02-17 04:05

Thanks to @titusmagnus for the answer, but I made one modification so as not to break scrolling when when the "disabled" scrollView is nested within another scrollView: You can't scroll the outer scrollView while the cursor is within the bounds of the inner scrollView. If you do this...

- (void)scrollWheel:(NSEvent *)theEvent
{
    [self.nextResponder scrollWheel:theEvent];
    // Do nothing: disable scrolling altogether
}

...then the "disabled" scrollView will pass the scroll event up to the outer scrollView and its scrolling will not get stuck down inside its subviews.

查看更多
登录 后发表回答