2014-02-26 50 views
0

我有一个NSTableView(基于视图)创建一个行;Autolayout在基于视图的NSTableView中

- (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; 
} 

此行创建一些子视图并分配一些约束;

- (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]; 
} 

乐趣开始当我真正尝试编辑约束.. viewWithBackground只是一个空的NSView,设置它的背景。当约束是| [viewWithBackground] |时对于水平和垂直方向,我都会得到预期的结果 - 绿色的行。当我将其更改为最基本的| - [viewWithBackground] - |时,我会得到一个意想不到的结果 - 红色的行,并且没有我的绿色视图标志!

我应该在这里采取一些额外的步骤吗?我的目标是让我的viewWithBackground实际上是一个稍微小一点的视图,以伪造行之间的“间隙”和距离桌面视图边缘的间距..

回答

1

如果有人曾经绊倒在这..事实证明那NSTableCellView是有点没有最小尺寸 - 在垂直约束上加上(> = 10)来处理这个问题。

相关问题