Show NSSegmentedControl menu when segment clicked, despite having set action

后端 未结 4 1520
清酒与你
清酒与你 2021-01-02 12:39

I have an NSSegmentedControl on my UI with 4 buttons. The control is connected to a method that will call different methods depending on which segment is clicked:



        
4条回答
  •  难免孤独
    2021-01-02 13:14

    I'm not sure of any built-in way to do this (though it really is a glaring hole in the NSSegmentedControl API).

    My recommendation is to continue doing what you're doing popping up the context menu. However, instead of just using the segmented control's origin, you could position it directly under the segment (like you want) by doing the following:

    NSPoint menuOrigin = [segmentedControl frame].origin;
    menuOrigin.x = NSMaxX([segmentedControl frame]) - [segmentedControl widthForSegment:4];
    // Use menuOrigin where you _were_ just using [segmentedControl frame].origin
    

    It's not perfect or ideal, but it should get the job done and give the appearance/behavior your users expect.

    (as an aside, NSSegmentedControl really needs a -rectForSegment: method)

提交回复
热议问题