Search code examples
cocoanstoolbarnstoolbaritemnssegmentedcontrol

Adjust width and placement of NSSegmentedControl with dynamic in toolbar


I've got a pretty dynamic UI that uses a NSSegmentedControl in the toolbar of its document window.

The NSSegmentedControl dynamically gets its elements assigned.

The number of elements can vary widely between 1 and maybe 15.

At the moment the toolbar will not adjust for the number of elements in the NSSegmentedControl, i.e. after setting the number of segments via setSegmentCount: all segments that exceed the original width of the NSSegmentedControl (at the time the window was created) are clipped:

enter image description here

What would be the best way to tell the NSToolbar to recalculate the width of all its elements, i.e. the width of the NSSegmentedControl and the placements of all elements right to it?


Solution

  • The clipping is caused by NSToolbarItem's current value for -maxSize. Try adjusting its width with - setMaxSize: after you add segments to the NSSegmentedControl or directly set the maximal width in Interface Builder.

    There is more info on this topic at https://developer.apple.com/library/mac/documentation/cocoa/Conceptual/Toolbars/Tasks/SettingTBItemSize.html