views:

795

answers:

4
+6  A: 

See the setAutoresizingMask: method of NSView and the associated resizing masks.

Martin Gordon
+1  A: 

See +NSView setAutoresizingMask:.

Chuck
+7  A: 

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:

tjw
+1  A: 

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];
e.James
Do you really find yourself doing this programmatically that much?
Chuck
Actually, 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