Disable scrolling in NSTableView

后端 未结 5 952
旧巷少年郎
旧巷少年郎 2020-12-17 19:05

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

It seems there isn\'t any property on [myTableView enclosingScrollView] or [[myTable

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-17 19:13

    Here's the best solution in my opinion:

    Swift 5

    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

提交回复
热议问题