Using interface builder you can select the corners an object should stick to when resizing. How can you do this programatically?
-
Please note that Apple introduced an [AutoLayout framework](http://developer.apple.com/library/mac/#releasenotes/UserExperience/RNAutomaticLayout/_index.html#//apple_ref/doc/uid/TP40010631) for Lion. – JJD Oct 18 '12 at 10:50
4 Answers
I find that the autoresizingBit
masks are horribly named, so I use a category on NSView to make things a little more explicit:
// MyNSViewCategory.h:
@interface NSView (myCustomMethods)
- (void)fixLeftEdge:(BOOL)fixed;
- (void)fixRightEdge:(BOOL)fixed;
- (void)fixTopEdge:(BOOL)fixed;
- (void)fixBottomEdge:(BOOL)fixed;
- (void)fixWidth:(BOOL)fixed;
- (void)fixHeight:(BOOL)fixed;
@end
// MyNSViewCategory.m:
@implementation NSView (myCustomMethods)
- (void)setAutoresizingBit:(unsigned int)bitMask toValue:(BOOL)set
{
if (set)
{ [self setAutoresizingMask:([self autoresizingMask] | bitMask)]; }
else
{ [self setAutoresizingMask:([self autoresizingMask] & ~bitMask)]; }
}
- (void)fixLeftEdge:(BOOL)fixed
{ [self setAutoresizingBit:NSViewMinXMargin toValue:!fixed]; }
- (void)fixRightEdge:(BOOL)fixed
{ [self setAutoresizingBit:NSViewMaxXMargin toValue:!fixed]; }
- (void)fixTopEdge:(BOOL)fixed
{ [self setAutoresizingBit:NSViewMinYMargin toValue:!fixed]; }
- (void)fixBottomEdge:(BOOL)fixed
{ [self setAutoresizingBit:NSViewMaxYMargin toValue:!fixed]; }
- (void)fixWidth:(BOOL)fixed
{ [self setAutoresizingBit:NSViewWidthSizable toValue:!fixed]; }
- (void)fixHeight:(BOOL)fixed
{ [self setAutoresizingBit:NSViewHeightSizable toValue:!fixed]; }
@end
Which can then be used as follows:
[someView fixLeftEdge:YES];
[someView fixTopEdge:YES];
[someView fixWidth:NO];

- 116,942
- 41
- 177
- 214
-
8Actually, yes. I've been working on a custom UI for a touch screen interface, and Interface Builder is simply not the right tool for that job. I've been doing everything in code, and I haven't looked back. – e.James May 09 '09 at 19:32
-
@Karolis: Glad to hear it. Thank you for taking the time to leave a note. – e.James Jun 10 '11 at 15:30
-
2So you mean it is possible to know what you're doing when you tell a view what to do when it's resized? If there's ever a Nobel Peace Prize for coding, you have my vote. – Elise van Looij Jun 26 '11 at 16:54
-
Ah, helps my intense dislike for autoresizing masks! Thanks for sharing. – sudo rm -rf Jul 07 '11 at 21:23
See the setAutoresizingMask: method of NSView and the associated resizing masks.

- 1
- 1

- 36,329
- 7
- 58
- 54
-
Here is a working link to the [resizing masks](https://developer.apple.com/documentation/appkit/nsview.autoresizingmask) – Aron Lindberg Jun 27 '17 at 12:29
Each view has the mask of flags, controlled by setting a the autoresizingMask property with the OR of the behaviors you want from the resizing masks. In addition, the superview needs to be configured to resize its subviews.
Finally, in addition to the basic mask-defined resizing options, you can fully control the layout of subviews by implementing -resizeSubviewsWithOldSize:

- 982
- 5
- 7
@e.James answer gave me an idea to simply create a new enum with more familiar naming:
typedef NS_OPTIONS(NSUInteger, NSViewAutoresizing) {
NSViewAutoresizingNone = NSViewNotSizable,
NSViewAutoresizingFlexibleLeftMargin = NSViewMinXMargin,
NSViewAutoresizingFlexibleWidth = NSViewWidthSizable,
NSViewAutoresizingFlexibleRightMargin = NSViewMaxXMargin,
NSViewAutoresizingFlexibleTopMargin = NSViewMaxYMargin,
NSViewAutoresizingFlexibleHeight = NSViewHeightSizable,
NSViewAutoresizingFlexibleBottomMargin = NSViewMinYMargin
};
Also, from my research, I discovered that @James.s has a serious bug in the NSView additions. The coordinate system in Cocoa has a flipped y-axis in terms of iOS coordinates system. Hence, in order to fix the bottom and top margin, you should write:
- (void)fixTopEdge:(BOOL)fixed
{ [self setAutoresizingBit:NSViewMaxYMargin toValue:!fixed]; }
- (void)fixBottomEdge:(BOOL)fixed
{ [self setAutoresizingBit:NSViewMinYMargin toValue:!fixed]; }
From the cocoa docs:
NSViewMinYMargin
The bottom margin between the receiver and its superview is flexible. Available in OS X v10.0 and later.

- 22,319
- 10
- 92
- 157
-
2+1 for the hybrid approach! I've never had problems with that code, but perhaps it is a difference between OSX and iOS? Now you have me curious... – e.James Dec 03 '13 at 22:58