Question

I have an NSTableView (view-based) that creates a row;

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    TaskTableCellView *tableCellView = [[TaskTableCellView alloc] init];
    return tableCellView;
}

-(void) tableView:(NSTableView *)tableView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row {
    NSView *view = [rowView viewAtColumn:0];
    [view setTranslatesAutoresizingMaskIntoConstraints:NO];
    NSDictionary *views = NSDictionaryOfVariableBindings(view);
    [tableView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:nil views:views]];
    [tableView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:nil views:views]];
}

- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {
    return 20;
}

This row creates some subviews and assigns some constraints;

- (void)layout {
    [super layout];
    ViewWithBackground *viewWithBackground = [[ViewWithBackground alloc] init];
    viewWithBackground.backgroundColor = [NSColor greenColor];
    [self addSubview:viewWithBackground];

    [viewWithBackground setTranslatesAutoresizingMaskIntoConstraints:NO];
    NSDictionary *views = NSDictionaryOfVariableBindings(viewWithBackground);
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[viewWithBackground]|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[viewWithBackground]|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:views]];

    [viewWithBackground setContentHuggingPriority:200 forOrientation:NSLayoutConstraintOrientationVertical];
    [viewWithBackground setContentHuggingPriority:200 forOrientation:NSLayoutConstraintOrientationHorizontal];
}


- (void)drawRect:(NSRect)dirtyRect {
    [[NSColor redColor] set];
    NSRectFill(dirtyRect);
    [super drawRect:dirtyRect];
}

The fun starts when I actually try to edit the constraints.. viewWithBackground is just an empty NSView that sets it's background. When the constraint is |[viewWithBackground]| for both horizontal and vertical, I get the expected result -- green rows. When I change it to the most basic |-[viewWithBackground]-|, I get a decidedly unexpected result -- red rows, and no sign of my green view!

Is there some additional step I'm supposed to take here? My goal is to have my viewWithBackground actually be a slightly smaller view, to fake the 'gaps' between rows and the spacing from the edges of the table view..

Was it helpful?

Solution

In case anyone ever stumbles on this.. it turns out that NSTableCellView is a bit wonky without a min size -- adding (>=10) on the Vertical constraint took care of the issue..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top