If the table has a fixed width, the easiest way is to just use a segmented control NSSegmentedControl
. First add it to your view or window:
Change its Style to Small Square
, the Mode to Select None
and increase the number of Segments to 4
(or keep it at 3
if you only need +
and -
):
The +, - and other buttons are predefined images of the AppKit framework (NSAddTemplate
and NSRemoveTemplate
) and directly available in Interface Builder. E.g. you could setup the first three segments as follows:
For demonstration purposes I made the -
segment disabled. Unlike most other buttons, disabling a segment of a segmented control only dims the image/text, it does not change the button background. Only disabling the whole segmented control changes the background (and of course disables all segments).
Of course, the last segment should always be disabled, otherwise it would be clickable and change its background when being clicked. As it contains no image or text, it still looks the same after it has been disabled.
Switch to the size setup and uncheck the Fixed
checkbox for all segments, except for the last one, make sure it is checked for the last one:
Unchecking Fixed
makes the segment width dynamic, that means it will always match the minimum width required for the content.
Finally place the control directly below the table and resize it to match the table width. Here's the result:
Almost perfect, don't you think?
Things get more difficulty if the table width is dynamic (e.g. the table resizes together with the window resizing). A segmented control doesn't support autoresizing, that means you would have to programmatically change the width of its last segment each time the table is resized. Of course, that is not too hard to do and requires only little code but there is one alternative solution that requires no single line of code.
Decrease the number of segments by one and replace the last segment with a gradient button (NSButton) without title:
Its background looks exactly the same as a segmented control, yet it does support autoresizing to always match the size of the table. There is just one problem: It is clickable and this time disabling it doesn't work as this would change the background. Instead just change its Type to Momentary Change
(which means the app wants to control the UI change itself when the button is clicked):
And after the button has been placed correctly and made resizable, the result looks as good as before but this time the table can be resizable and the buttons on the bottom will always fit perfectly.