macoscocoanstableviewnstablecolumn

Resize Table Column when NSWindow resizes


I am working on a Cocoa application in which I am using NSTableView.

Now I want the widths of the columns in the table view to be auto-resized when the user resizes or maximizes the window, by either dragging the application from a corner, or by clicking the zoom button.

Any help will be appreciated.


Solution

  • Make sure your table view itself resizes when the window resizes, by setting up constraints or setting its autoresizing mask.

    Then set the table's overall column autoresizing style. In a storyboard or XIB, you do this by selecting the table view (not the scroll view that contains it) and then setting the “Column Sizing” popup in the Attributes inspector. In code, you set the table view's columnAutoresizingStyle. The different styles are described under NSTableViewColumnAutoresizingStyle. If you want all of the columns to expand or contract during a resize, set it to NSTableViewUniformColumnAutoresizingStyle.

    Then set up each column's autoresizing behavior if necessary.

    If you are setting up your table in a XIB or storyboard, you can select the column and then set its resizing behavior under the Attributes inspector.

    If you are creating a column in code, set the resizingMask of each NSTableColumn to include whichever resizing mode bits you want. The default is to allow both user resizing and autoresizing, so you shouldn't need to do anything.