Cocoa: change cursor when it's over an NSButton

Matt S. picture Matt S. · May 28, 2010 · Viewed 9.9k times · Source

How can I change the cursor when it's over an NSButton?

Answer

6 1 picture 6 1 · Nov 7, 2012

You should subclass NSButton first, then add the code below.

- (void)resetCursorRects
{
    if (self.cursor) {
        [self addCursorRect:[self bounds] cursor: self.cursor];
    } else {
        [super resetCursorRects];
    }
}

Now you can set cursor as you like.

[self.button setCursor:[NSCursor pointingHandCursor]];

Note: add cursor as a property of your subclass, like this:

@property (strong) NSCursor *cursor;