뷰 기반의 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];
}
.
실제로 제약 조건을 편집하려고 할 때 재미가 시작됩니다. ViewWithBackground는 배경을 설정하는 빈 NSView입니다.제약 조건이 | [ViewWithBackground] |수평 및 수직을 위해 예상 결과 - 녹색 행을 얻습니다.내가 가장 기본적인 정보로 바꿀 때 - [ViewWithBackground] - | 나는 결정적으로 예기치 않은 결과 - 빨간색 행, 녹색 뷰의 흔적이 없어!
여기에서 가져 가려고하는 추가 단계가 있습니까?내 목표는 내 ViewWithBackground가 실제로 약간 더 작은보기가되어 테이블보기의 가장자리에서 간격 사이의 간격을 가짜로 만들기 위해 실제로 약간 더 작은보기를 갖는 것입니다.
해결책
누군가가 이런 일을 방해하는 경우가 있습니다. NSTableCellView가 최소 크기가없는 조금 Wonky임을 밝혀졌습니다. 세로 제약 조건에 문제가 해결되었습니다.
제휴하지 않습니다 StackOverflow