18
votes

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

5
All the answers mentioned here do not work when the button is above TextView. For this case refer stackoverflow.com/questions/16287624/…Kaunteya

5 Answers

41
votes

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;  
17
votes
[yourButton addCursorRect:[yourButton bounds] cursor:[theCursorYouWant]];
3
votes

Following the already given example, creating a subclass of NSButton in Swift 5:

import AppKit

class Button: NSButton {
    override func resetCursorRects() {
        addCursorRect(bounds, cursor: .pointingHand)
    }
}
-1
votes

If NSButton in macOS, swift 5:

import Cocoa 
class ViewController: NSViewController {

    @IBOutlet weak var titleBtn: NSButton!
        
     override func viewDidLoad() {
       titleBtn.addCursorRect(titleBtn.bounds, cursor: .pointingHand)
     }
}
-4
votes

Have the button add a cursor rect.