Show NSSegmentedControl menu when segment clicked, despite having set action
My question is based on another question. Instead of repeating a bunch of stuff, I hope it's okay that I refer you to that other question instead:
Show NSSegmentedControl menu when segment clicked, despite having set action
My question is: How would the code within [self showGearMenu] look like, i.e. how would 开发者_StackOverflow社区one actually use the popUpContextMenu:withEvent:forView method to accomplish what the poster of the other question wants?
I ask because it seems that the question was resolved for the poster, but he never shared with us how he actually popped up the menu :)
http://www.sethwillits.com/temp/AGNSSegmentedControl.h (also .m)
I had this question a couple months ago, which I asked on Cocoa-dev:
http://www.cocoabuilder.com/archive/cocoa/275771-trouble-with-nssegmentedcontrol.html#275771
The code that was emailed to me off-list works great, but it's not my code, so I'm going to post it. However if you'd like it, contact Seth at Araelium Group about AGNSSegmentedControl
: http://www.araelium.com/
I ended up having to do this, and the following looks almost exactly like the system presentation:
final class MenuSegmentedControl: NSSegmentedControl {
@objc
func showMenu() {
var menuOrigin = frame.origin
// magic number that gives just the right spacing between the button and the menu
menuOrigin.y -= 4
// this takes advantage of the enclosing menu item
menu(forSegment: 0)?.popUp(positioning: enclosingMenuItem, at: menuOrigin, in: superview)
}
}
I only have a single segment, though. This is a toolbar item that's meant to look like the Flag segment in Mail.app. In the action
, it calls showMenu()
on the segmented control.
精彩评论