How can I change the cursor when it's over an NSButton?
Asked
Active
Viewed 1.1k times
18
-
All the answers mentioned here do not work when the button is above TextView. For this case refer https://stackoverflow.com/questions/16287624/how-to-force-the-cursor-to-be-an-arrowcursor-when-it-hovers-a-nsbutton-that-is – Kaunteya Apr 25 '20 at 06:04
5 Answers
41
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;
-
1Could you expand a little on your answer. NSButton doesn't have a cursor instance variable? I don't understand your answer. – Daniel Farrell Jan 27 '13 at 12:57
-
Subclassing each and every button I have in the UI just to apply a hovering cursor seem unreasonable. After telling you you shouldn't call addCursorRect directly, docs also tell you never to call "resetCursorRects" directly, and only call "invalidateCursorRects" which is an NSWindow method. – Motti Shneor Apr 07 '14 at 21:54
-
2@MottiShneor Calling [super resetCursorRects] within -resetCursorRects itself doesn't violate the "never call this" rule as you're not actually invoking it when it was supposed to anyway. – Oliver Cooper Aug 07 '15 at 12:39
-
@MottiShneor Surely you'd subclass only once then use the subclass wherever you want the behaviour. – fractor Aug 08 '20 at 15:48
17
[yourButton addCursorRect:[yourButton bounds] cursor:[theCursorYouWant]];

maschall
- 988
- 1
- 7
- 13
-
17From Apple docs: _This method is intended to be invoked only by the resetCursorRects method. If invoked in any other way, the resulting cursor rectangle will be discarded the next time the view's cursor rectangles are rebuilt_. So you have to subclass NSButton. – Kentzo Apr 03 '12 at 07:09
4
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)
}
}

vicegax
- 4,709
- 28
- 37
-
2I recommend calling `super.resetCursorRects()` prior to calling `addCursorRect(bounds, cursor: .pointingHand)` – Patrick Mar 01 '20 at 11:00
-1
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)
}
}

Nikolay
- 127
- 2
- 3
-4
Have the button add a cursor rect.

Chuck
- 234,037
- 30
- 302
- 389
-
6Was the down-vote necessary? A quick search of the documentation for "cursor rect" tells you plenty. – Joshua Nozzi May 27 '10 at 23:43
-
2
-
1@BenLeggiero You have a pretty odd definition of "useful". Given Chuck's answer was correct but merely brief, I think a downvote is a bit heavy-handed. Other SO'ers have the option to write a better answer or upvote one. But sure, let's punish attempts at helping others. – Joshua Nozzi Aug 05 '16 at 14:57
-
2@JoshuaNozzi This is my opinion, and it might not be shared by others on SO: To be useful, and answer should stand on its own. If it is tied to using an API, it should provide at least one example (in the answer text itself) of how to use that API. Otherwise, it should provide example code for how to do it yourself. IMHO, answers like this are just as good as posting a link to a Google search. – Ky - Aug 05 '16 at 17:21
-
@BenLeggiero A prime example of what drives knowledgable volunteers away from the site. Why participate if you're punished for "helping, just not enough for someone's taste"? – Joshua Nozzi Aug 05 '16 at 22:32
-
2@JoshuaNozzi I'm just going by the site-wide [answering guidelines](http://stackoverflow.com/help/how-to-answer), which say things like "Brevity is acceptable, but fuller explanations are better," and "try to mention any limitations, assumptions or simplifications". – Ky - Aug 08 '16 at 20:48