Cocoa: change cursor when it's over an NSButton

前端 未结 5 767
一整个雨季
一整个雨季 2020-12-01 14:26

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

相关标签:
5条回答
  • 2020-12-01 15:00

    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;  
    
    0 讨论(0)
  • 2020-12-01 15:07
    [yourButton addCursorRect:[yourButton bounds] cursor:[theCursorYouWant]];
    
    0 讨论(0)
  • 2020-12-01 15:09

    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)
         }
    }
    
    0 讨论(0)
  • 2020-12-01 15:16

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-01 15:21

    Have the button add a cursor rect.

    0 讨论(0)
提交回复
热议问题