Cocoa:当它在NSButton上时更改光标

问题描述 投票:15回答:4

当它在NSButton上时,如何更改光标?

cocoa cursor nsbutton
4个回答
17
投票
[yourButton addCursorRect:[yourButton bounds] cursor:[theCursorYouWant]];

39
投票

您应该首先继承NSButton,然后添加下面的代码。

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

现在您可以根据需要设置光标。

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

注意:添加cursor作为子类的属性,如下所示:

@property (strong) NSCursor *cursor;  

1
投票

在已经给定的example之后,在Swift 5中创建NSButton的子类:

import AppKit

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

-1
投票

让按钮添加光标rect。

© www.soinside.com 2019 - 2024. All rights reserved.