2012-10-02 4 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. 

자동으로 생성되는 것처럼 보입니다.

내 생성 코드 (사용 아크) 내가 여기
-(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]; 

} 

NSView의

서브 클래스 해요되어이 파괴되는 곳 :

-(void)setFrame:(NSRect)frameRect{ 
    [super setFrame:frameRect]; 
    [self.scrollView setFrame:self.bounds]; 

} 

이 자동 제약을 해제하는 방법이 있나요?

답변

7

보기의 자동 크기 조절 마스크를 기반으로 자동 구속 조건이 작성되는 것이 문제입니다. 있는 ScrollView가 NSView 서브 클래스의 DrawRect 방법에서, NSScrollViewframe를 설정할 때 나는 또한 동일한 문제를 가지고 NSView의

0

의 하위 뷰

[scrollView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

:이 동작을 비활성화합니다. TranslatesAutoresizingMaskIntoConstraints을 NO로 설정하는 위의 솔루션은 예외를 제거합니다. 그러나 제 상황에서는 동적 인 창이 있습니다. 실제로, tableview은 더 이상 계산하지 않고 NSScrollView으로 설정하는 새 프레임 크기를 따르지 않았습니다.

잠시 동안 작업하면서 마침내 올바른 해결책을 찾았습니다. 문제는 NSScrollView 자동 설정에 관한 것이 아니지만 (내 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 
} 

는 또한 또한 NSScrollViewframe 설정 요구를했다 프로그래밍 다른 NSView 서브 클래스를 만들어야합니다. 이러한 객체에는 awakeFromNib 메서드가 없으므로 ... 단순히 다른 위치에 설정하고 & hellip; NSView 하위 클래스의 Init 메서드와 비슷합니다.

- (id)init:(NSInteger)aSpecialTag 
{ 

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

    _mTag = aSpecialTag; 

    // … 
} 
관련 문제