Search code examples
objective-cnsbutton

How can I permanently change the cursor for an NSButton?


I've set the cursor for my NSButton using the technique in this question, and that works great initially. However, it breaks as soon as the window is hidden and then re-shown (this is a menubar app, so the window gets hidden every time the user clicks outside the window). At that point, it goes back to the normal cursor and will never go back to the pointing hand.

Is there a way to permanently set the cursor for an NSButton?


Update:
I've found that it isn't, so much, the showing/hiding of the window that kills the cursor as it is moving to another app and then moving back. If I simply show/hide the window, by clicking on the menubar icon over and over, the cursor always behaves. But as soon as I click to another app and then click back, the cursor is forever broken (until I kill and re-start the app).


Solution

  • I found that the problem was that the app was not being brought to the front when the user clicked on it. It was initially in the front, just after starting the app, but clicking away and then clicking back was making it appear in front, but it was NOT making it the "active window", for some reason.

    I got around the issue by programmatically forcing it to the front using either

    [[NSApplication sharedApplication] activateIgnoringOtherApps : YES];
    

    or

    [[NSRunningApplication currentApplication] activateWithOptions:(NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps)];
    

    as pointed out in this question.