Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTableView align resize columns

I need to create a NSTableView with 3 columns and these columns have to fit the space like this form 20% 60% 20%. I set the constraints in the interface builder but no success. The result is always the same. The 3 columns are shown but in the same size as in the IB, and if i resize the window the table resizes too but the columns keep with the original size. How can i resize the columns proportionally to tableview size? How can i align one column always to the right? Need help please. My project is for OS X, not iPhone. Thanks in advance.

like image 447
user3065901 Avatar asked May 19 '26 19:05

user3065901


1 Answers

I'm not sure what constraints you tried to set, but table columns are not views, so they don't participate in auto layout.

There's no automatic support for what you're trying to achieve. NSTableView can be set to distribute a change of width equally among its columns (setting columnAutoresizingStyle to NSTableViewUniformColumnAutoresizingStyle), but that doesn't maintain the proportions like you want.

I think you want to set the table view so that it doesn't resize its columns. Then you would monitor it for any changes in its frame and adjust the column widths yourself.

You can set the column autoresizing style of the table view in IB.

To monitor the frame, you can either use a subclass of NSTableView and override -setFrameSize: or you can have some controller observe the NSViewFrameDidChangeNotification notification from the table view. Either way, you'd want to calculate 20% and 60% of the width. You probably want to round or truncate to an integer. Also, you should use percentages only for two of the three columns. For the third, subtract the width you computed for the first two from the table view's width. That's the easiest way to be sure they all add up.

For each column, check if the width you calculated is different from its current width and, if so, set its width.

like image 131
Ken Thomases Avatar answered May 24 '26 16:05

Ken Thomases



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!