Subview of custom NSView is resizing itself to fit within its superview
- by Jonathan Patt
I have a custom NSView which contains a subview—for the purposes of solving this issue, the top level view is simply acting as a container and automatically resizes to fit within the window. Its subview draws a rectangle 10 pixels inset from the edge of the window, except for the bottom, which should get clipped by the window until it's resized to show the entire rectangle. It does not, however, behave as I intend, and instead shrinks the height of the rectangle when its superview has a smaller height than it does. How do I avoid this and have it stay at its proper height and instead get clipped when the superview is smaller?
The parent view's implementation code is:
- (id)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
subView = [[SubView alloc] initWithFrame:frameRect];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
[[NSColor grayColor] set];
[subView setFrame:NSMakeRect(10, 10, dirtyRect.size.width - 20, 500)];
[self addSubview:subView];
}
- (BOOL)isFlipped {
return YES;
}
And the subview is just:
- (void)drawRect:(NSRect)dirtyRect {
[[NSColor blackColor] set];
[NSBezierPath strokeRect:dirtyRect];
}
- (BOOL)isFlipped {
return YES;
}