Tuesday, February 19, 2013

NSView AutoresizingMask explained

You can make your NSViews auto arrange/resize according to its superview's size by setting appropriate values using NSView's setAutoresizingMask: method. For this example's sake I'll consider elements added as subviews to the contentView of the NSWindow. NSWindow can change its size when going into full screen, when the user clicks on the resize control (green button) and when the user manually resize the window. For a better UI experience it is required to re-arrange/re-size the elements according to the change in size of the NSWindow. Below is a list of possible parameters which could be passed into setAutoresizingMask: and an explanation of the results.

NSViewWidthSizable: Use this if you want to change the element's width proportionally when the window resizes.

NSViewHeightSizable: Use this if you want to change the element's height proportionally when the window resizes.

NSViewMaxYMargin: Use this if you want to change the element's top margin proportionally when the window resizes.

NSViewMaxXMargin: Use this if you want to change the element's right margin proportionally when the window resizes.

NSViewMinYMargin: Use this if you want to change the element's bottom margin proportionally when the window resizes.

NSViewMinXMargin: Use this if you want to change the element's left margin proportionally when the window resizes.

Passing of a combination of two or more above parameters is also possible. For example if you want your element to change its width and height according to change in window size, the following code could be used.

[myView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];

No comments: