2012-10-02 41 views
4

我以編程方式在NSScrollView內創建NSTableView,但是當我嘗試將框架設置爲滾動視圖時,出現約束錯誤。NSScrollView中的自動佈局約束條件

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

} 

有沒有辦法關閉這些自動限制?

回答

7

明白了,問題是基於視圖的自動調整大小掩碼創建自動約束。要停用此行爲:

[scrollView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

其中滾動視圖是一個NSView

0

的子視圖設置的NSScrollViewframe時,一個NSView子類的DrawRect方法中我也得到了完全相同的問題。上述將TranslatesAutoresizingMaskIntoConstraints設置爲NO的解決方案擺脫了這個例外。但在我的情況下,我有一個動態的窗口。事實上,我的tableview沒有遵循我正在計算的新幀大小,並且已經設置爲NSScrollView了。

解決了一會兒,我終於想出了正確的解決方案。看起來問題不在於NSScrollView自動調整設置,但設置我的NSView子類的AutoresizesSubviews是動態自定義視圖的正確解決方案。

- (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編程,這也有NSScrollViewframe設置需求。由於這些對象沒有awakeFromNib方法...只需將它設置在其他地方&hellip;就像在NSView子類的Init方法中一樣。

- (id)init:(NSInteger)aSpecialTag 
{ 

[self setAutoresizesSubviews:NO]; // <- Here is the solution 

    _mTag = aSpecialTag; 

    // … 
}