基于视图的NSTableView内的Autolayout
-
21-12-2019 - |
题
我有一个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];
}
.
当我实际尝试编辑约束时,乐趣开始。查看WhithBackground只是一个空的NSView,可以设置它的背景。当约束是| [查看Whithgrackground] |对于水平和垂直,我得到了预期的结果 - 绿色行。当我将其更改为最基本的| - [查看Whithgrackground] - |,我得到一个明显的意外结果 - 红色行,没有我的绿色视图的迹象!
是否有一些额外的步骤,我应该在这里来?我的目标是让我的观点实际上是一个略微较小的观点,以伪造行之间的“差距”以及从表视图的边缘之间的间距..
解决方案
如果有人陷入困境。事实证明,NSTableCellView在没有Min大小的情况下,NSTableCellView是一点 - 在垂直约束上添加(>= 10)照顾问题..
不隶属于 StackOverflow