我已涉足这个领域有一段时间了...我以为这应该是一个简单的任务,但它并不是 =D
我想做的是,在用户点击组合框时显示组合框列表,而不是特定地在按钮中。
有什么想法吗? 提前感谢!
我已涉足这个领域有一段时间了...我以为这应该是一个简单的任务,但它并不是 =D
我想做的是,在用户点击组合框时显示组合框列表,而不是特定地在按钮中。
有什么想法吗? 提前感谢!
@interface NSComboBox (MYExpansionAPI)
@property (getter=isExpanded) BOOL expanded;
@end
@implementation NSComboBox (MYExpansionAPI)
- (BOOL) isExpanded
{
id ax = NSAccessibilityUnignoredDescendant(self);
return [[ax accessibilityAttributeValue:
NSAccessibilityExpandedAttribute] boolValue];
}
- (void) setExpanded: (BOOL)expanded
{
id ax = NSAccessibilityUnignoredDescendant(self);
[ax accessibilitySetValue: [NSNumber numberWithBool: expanded]
forAttribute: NSAccessibilityExpandedAttribute];
}
我在我的controlTextDidChange:
方法中使用了这段代码。
- (void) controlTextDidChange:(NSNotification *) aNotification {
NSTextField *textField = [aNotification object];
NSString *value = [textField stringValue];
NSComboBox *box = [self comboBox];
if (value == nil || [value length] == 0) {
if ([box isExpanded]) { [box setExpanded:NO]; }
} else {
if (![box isExpanded]) { [box setExpanded:YES]; }
}
}
Returns true
if the NSComboBox's list is expanded
comboBox.cell?.isAccessibilityExpanded() ?? false
Open the NSComboBox's list
comboBox.cell?.setAccessibilityExpanded(true)
Close the NSComboBox's list
comboBox.cell?.setAccessibilityExpanded(false)
参考 jmoody的回答。
[(NSComboBoxCell*)self.acomboBox.cell performSelector:@selector(popUp:)];
放置
comboBoxCell.performSelector(Selector("popUp:"))
进入
override func controlTextDidChange(obj: NSNotification) {}
这是我最终获得的代码。感谢@Ahmed Lotfy
以下是完整的代码,它在我的OSX 10.11上可用。
override func controlTextDidChange(obj: NSNotification) {
if let comboBoxCell = self.comboBox.cell as? NSComboBoxCell {
comboBoxCell.performSelector(Selector("popUp:"))
}
}
import Cocoa
class CComboBoxEx: NSComboBox
{
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
// Drawing code here.
}
func isExpanded() -> Bool{
if let ax:AnyObject? = NSAccessibilityUnignoredDescendant(self) {
if ax!.accessibilityAttributeValue(NSAccessibilityExpandedAttribute) != nil {
return true
}
}
return false
}
func setExpanded (bExpanded:Bool) {
if let ax:AnyObject? = NSAccessibilityUnignoredDescendant(self) {
ax!.accessibilitySetValue(NSNumber(bool: bExpanded), forAttribute: NSAccessibilityExpandedAttribute)
}
}
}
NSComboBox并不是设计成这样工作的。因为用户可能想要编辑控件中的文本,他们需要能够单击它而不会意外地弹出选项。
您需要子类化NSComboBoxCell并更改此行为...但然后您将拥有一个外观标准但行为不标准的控件。如果您决心这样做,请查看NSComboBoxCell的开源版本。有趣的方法似乎是-popUpForComboBoxCell:和friends。
根据其他答案,我编写了这个解决方案(在Xcode 10.2.1,Swift 5中测试)。它使用相同的思路,但代码稍微更短。
// Put this extension for NSComboBox somewhere in your project
import Cocoa
public extension NSComboBox {
var isExpanded: Bool{
set {
cell?.setAccessibilityExpanded(newValue)
}
get {
return cell?.isAccessibilityExpanded() ?? false
}
}
}
// Set your corresponding NSViewController as NSComboBoxDelegate
// in the storyboard and add this piece of code
// to expand the combobox when the user types
class MyViewController: NSViewController, NSComboBoxDelegate {
func controlTextDidChange(_ notification: Notification) {
guard let comboBox = notification.object as? NSComboBox else { return }
if comboBox.isExpanded == false {
comboBox.isExpanded = true
}
}
}