NSView(MOSizing)


Abstract

NSView extension methods to implement min and max sizing behaviors.

Discussion

NSView extension methods implementing enforced minimum and maximum sizes for views. Also supports a view automatically getting its minimum size from the size of the NSClipView it is a subview of.

This category adds capabilities to all NSViews but does not affect the normal behavior of a view unless one or more of the "set" methods in the category are used. The category is implemented using the method replacement methods from MORuntimeUtilties. Specifically, +[NSObject MO_replaceInstanceSelector:withMethodForSelector:] is used to replace five NSView methods: -dealloc, -setFrame:, -setFrameSize:, -viewWillMoveToSuperview: and -viewDidMoveToSuperview.

Method replacement is not to be taken lightly, although done properly it should be safe. But becuase it is a slightly dicey business there are two mechanisms in MOKit to disable features that use method replacement.

One is a build time switch: if the macro MOKIT_NO_METHOD_REPLACEMENT is defined (via -DMOKIT_NO_METHOD_REPLACEMENT in the compiler flags) then no features of MOKit that use method replacement will be compiled into the framework. The public API that depends on it will still be present but will be ineffective.

The other is a runtime switch: if the user default MOKitAllowMethodReplacement is set to NO (it defaults to YES) then no method replacement will be done. Again, the public API that depends on it will still be present but will be ineffective.

Note that the minimum and maximum sizes and the setting for whether the min size tracks the clip view are NOT coded when a view is archived and so cannot be saved in a nib file.



Methods

MO_minSize
Returns the minimum size for a view.
MO_setMinSize:
Sets the minimum size for a view.
MO_maxSize
Returns the maximum size for a view.
MO_setMaxSize:
Sets the maximum size for a view.
MO_takesMinSizeFromClipView
Returns whether the minimum size of the view should be the bounds size of the clip view it is in.
MO_setTakesMinSizeFromClipView:
Sets whether the minimum size of the view should be the bounds size of the clip view it is in.
MO_sizeConstraintsDidChange
This method is called whenever the min or max size contraints on a view may have changed.

MO_maxSize

Returns the maximum size for a view.
- ( NSSize ) MO_maxSize;

This method returns the maximum size of a view. If MO_setMaxSize: has been previously called, this returns the size that was set with that method. Otherwise, this returns the size {MAXFLOAT, MAXFLOAT}.

method result
The maximum size of the receiver.

MO_minSize

Returns the minimum size for a view.
- ( NSSize ) MO_minSize;

This method returns the minimum size of a view. If MO_takesMinSizeFromClipView is YES and the view is currently the direct subview of an NSClipView, then this method returns the current bounds size of the clip view. If MO_setMinSize: has been previously called, this returns the size that was set with that method. Otherwise, this returns the size {0.0, 0.0}.

method result
The minimum size of the receiver.

MO_setMaxSize:

Sets the maximum size for a view.
- ( void ) MO_setMaxSize:
        (NSSize ) maxSize;

This method sets the maximum size of a view.

Parameter Descriptions
maxSize
The maximum size to be used for the receiver.

MO_setMinSize:

Sets the minimum size for a view.
- ( void ) MO_setMinSize:
        (NSSize ) minSize;

This method sets the minimum size of a view. If MO_takesMinSizeFromClipView is YES and the view is currently the direct subview of an NSClipView, then the size set with this method will have no effect and the minimum size will instead be the size of the clip view's bounds rectangle.

Parameter Descriptions
minSize
The minimum size to be used for the receiver.

MO_setTakesMinSizeFromClipView:

Sets whether the minimum size of the view should be the bounds size of the clip view it is in.
- ( void ) MO_setTakesMinSizeFromClipView:
        (BOOL ) flag;

This method sets whether the minimum size of the view should be the bounds size of the clip view it is in. If set to YES and the view is a direct subview of an NSClipView then MO_minSize will return the bounds size of the clip view. This setting has no effect when the receiver is not a subview of an NSClipView.

This method is often called from an NSView subclass' -initWithFrame: override. For example MOViewListView does this since it generally expects to live inside an NSClipView. Views that use this feature are encouraged to have a -sizeToFit method. When the clipview's size changes and the minimum size of the view is updated, if the view implemented -sizeToFit, it will be called. This allows the view to shrink itself if its minSize just got smaller and it was bigger than it might naturally want to be, for example.

Parameter Descriptions
flag
YES if the view's minimum size should be the bounds size of its clip view, NO if not.

MO_sizeConstraintsDidChange

This method is called whenever the min or max size contraints on a view may have changed.
- ( void ) MO_sizeConstraintsDidChange;

This method is called whenever the min or max size contraints on a view may have changed. You should never call it directly, but you may wish to override it. A common override of this method would invoke -sizeToFit to make sure the view is as close to its natural size as possible given the new contraints. The default implementation does nothing.


MO_takesMinSizeFromClipView

Returns whether the minimum size of the view should be the bounds size of the clip view it is in.
- ( BOOL ) MO_takesMinSizeFromClipView;

This method returns whether the minimum size of the view should be the bounds size of the clip view it is in. If this is YES and the view is a direct subview of an NSClipView then MO_minSize will return the bounds size of the clip view. This setting has no effect when the receiver is not a subview of an NSClipView.

method result
YES if the view's minimum size should be the bounds size of its clip view, NO if not.

(Last Updated 3/20/2005)