我正在以编程方式在NSScrollView中创建NSTableView,但是当我尝试将框架设置为滚动视图时,我遇到了约束错误。
Unable to simultaneously satisfy constraints:
(
"<NSAutoresizingMaskLayoutConstraint:0x107d4ec50 h=--& v=--& V:[NSScrollView:0x10068c570(372.5)]>",
"<NSAutoresizingMaskLayoutConstraint:0x107d4d020 h=-&- v=-&- V:[NSClipView:0x10068d7f0]-(673)-| (Names: '|':NSScrollView:0x10068c570 )>",
"<NSAutoresizingMaskLayoutConstraint:0x107d4cfc0 h=-&- v=-&- V:|-(2)-[NSClipView:0x10068d7f0] (Names: '|':NSScrollView:0x10068c570 )>"
)
Will attempt to recover by breaking constraint
<NSAutoresizingMaskLayoutConstraint:0x107d4d020 h=-&- v=-&- V:[NSClipView:0x10068d7f0]-(673)-| (Names: '|':NSScrollView:0x10068c570 )>
Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, break on objc_exception_throw to catch this in the debugger.
好像他们是自动创建的。
这是我的创建代码(使用arc)我是子类化NSView
-(void)addColumnsToTable{
NSTableColumn *col = [[NSTableColumn alloc]initWithIdentifier:@"priority"];
[col.headerCell setStringValue:@"priority"];
[col setWidth:10];
[col setMinWidth:1];
[col setMaxWidth:10];
[self.tableView addTableColumn:col];
// col = [[NSTableColumn alloc]initWithIdentifier:@"name"];
// [self.tableView addTableColumn:col];
}
-(void)createTable{
NSTableView *tableView = [[NSTableView alloc]initWithFrame:NSZeroRect];
[tableView setDelegate:self];
[tableView setDataSource:self];
NSScrollView *scrollView = [[NSScrollView alloc]initWithFrame:self.bounds];
[scrollView setBorderType:NSGrooveBorder];
[self addSubview:scrollView];
[scrollView setDocumentView:tableView];
[scrollView setHasVerticalScroller:YES];
[scrollView setAutohidesScrollers:YES];
self.tableView = tableView;
self.scrollView = scrollView;
[self addColumnsToTable];
}
这是它破裂的地方:
-(void)setFrame:(NSRect)frameRect{
[super setFrame:frameRect];
[self.scrollView setFrame:self.bounds];
}
有没有办法关闭这些自动约束?
答案 0 :(得分:7)
知道了,问题是正在根据视图的自动调整大小掩码创建自动约束。要停用此行为:
[scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
其中scrollView是NSView的子视图
答案 1 :(得分:0)
在frame
子类的NSScrollView
方法中设置DrawRect
的{{1}}时,我也遇到了完全相同的问题。将NSView
设置为NO的上述解决方案摆脱了异常。但在我的情况下,我有一个动态窗口。实际上,我的TranslatesAutoresizingMaskIntoConstraints
没有按照我正在计算的新框架尺寸并再次设置为tableview
。
解决了一段时间,我终于想出了正确的解决方案。问题似乎与NSScrollView
自动调整设置无关,但设置NSScrollView
子类的AutoresizesSubviews
是动态自定义视图的正确解决方案。
NSView
我还必须以编程方式创建其他- (void)awakeFromNib
{
[super awakeFromNib];
[self setAutoresizesSubviews:NO]; // <- Here is the solution
// …
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
// From DirtyRect (dimensions of the my subclass NSView)
// Calculate the rectangle allowable for MyScrollView
// and setting local NSRect mRect..
[MyScrollView setFrame:mRect]; // no more ‘exception’ here
}
子类,这也需要NSView
和NSScrollView
设置。由于这些对象没有frame
方法...只需将其设置在其他位置......就像在awakeFromNib
子类的Init方法中一样。
NSView