gpt4 book ai didi

cocoa - 基于 View 的 NSTableView 内的自动布局

转载 作者:行者123 更新时间:2023-12-03 17:52:23 25 4
gpt4 key购买 nike

我有一个 NSTableView(基于 View )来创建一行;

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

该行创建一些 subview 并分配一些约束;

- (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]-| 时,我得到了一个绝对意想不到的结果 - 红色行,并且没有绿色 View 的迹象!

我应该在这里采取一些额外的步骤吗?我的目标是让我的 viewWithBackground 实际上成为一个稍小的 View ,以伪造行之间的“间隙”以及表格 View 边缘的间距..

最佳答案

万一有人无意中发现了这一点..事实证明,如果没有最小尺寸,NSTableCellView 就有点奇怪——在垂直约束上添加 (>=10) 就可以解决这个问题..

关于cocoa - 基于 View 的 NSTableView 内的自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22052279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com