18

I have a custom View NSView and I want to disable userinteraction, but I'm not sure how to do this.

My idea was:

[myView setEnabled:NO];

but it's wrong and doesn't work. How can I make it so that, it's just visible for the user, and nothing else?

Ben Close
  • 414
  • 1
  • 7
  • 18
Viper OS X
  • 291
  • 1
  • 5
  • 14

5 Answers5

16

NSView doesn't have either setEnabled: or setIgnoresMouseEvents:

Implement the hitTest: method to return nil.

Ravindhiran
  • 5,304
  • 9
  • 50
  • 82
  • I like this solution. Simple to implement and way more robust and reliable than disabling the subviews if they are controls. – DarkDust Feb 18 '15 at 08:18
  • This did not work for me at all. Implementing mouseDown: that does nothing is the solution in my case. – ghr May 31 '17 at 00:25
  • 1
    That would prevent mouse interactions but we'd want to disable any interaction, including via the keyboard (which is possible by enabling keyboard navigation with ctrl-F7, then using the Tab and space keys) – Thomas Tempelmann May 26 '19 at 17:35
3

From here:

//
//  NSView-DisableSubsAdditions.m
//  Can Combine Icons
//
//  Created by David Remahl on Tue Dec 25 2001.
//  Copyright (c) 2001 Infinity-to-the-Power-of-Infinity. All rights reserved.
//

#import "NSView-DisableSubsAdditions.h"

@implementation NSView(DisableSubsAdditions)

- (void)disableSubViews
{
    [self setSubViewsEnabled:NO];
}

- (void)enableSubViews
{
    [self setSubViewsEnabled:YES];
}

- (void)setSubViewsEnabled:(BOOL)enabled
{
    NSView* currentView = NULL;
    NSEnumerator* viewEnumerator = [[self subviews] objectEnumerator];

    while( currentView = [viewEnumerator nextObject] )
    {
        if( [currentView respondsToSelector:@selector(setEnabled:)] )
        {
            [(NSControl*)currentView setEnabled:enabled];
        }
        [currentView setSubViewsEnabled:enabled];

        [currentView display];
    }
}

@end
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
3

subclass the NSView and add following method

-(void)mouseDown:(NSEvent *)theEvent
{

}
Muruganandham K
  • 5,271
  • 5
  • 34
  • 62
  • Perfect. hitTest: returning nil does not work for me at all - it seemed to prevent mouseDown being invoked. So just use an empty mouseDown: – ghr May 31 '17 at 00:27
  • That would prevent mouse interactions but we'd want to disable any interaction, including via the keyboard (which is possible by enabling keyboard navigation with ctrl-F7, then using the Tab and space keys) – Thomas Tempelmann May 26 '19 at 17:35
2

Here is an example in Swift 4. Place your views into this custom view.

class UserInterectionDisabledView: NSView {
    override func hitTest(_ point: NSPoint) -> NSView? {
        return nil
    }
}
Joey
  • 2,912
  • 2
  • 27
  • 32
0

I think overriding hitTest(_:) is not ideal as it can block all interactions behind it.

A better way is to make the view refuse to becomeFirstResponder. So that events can be send to next responder, basically it skips the view.

class SubclassNSView: NSView {

  public var isUserInteractionEnabled: Bool = true

  open override func becomeFirstResponder() -> Bool {
    if isUserInteractionEnabled == false {
      return false
    } else {
      return super.becomeFirstResponder()
    }
  }
}
Honghao Z
  • 1,419
  • 22
  • 29