2014-04-09 2 views
1

Cocoa Autolayout에 몇 가지 문제가 있습니다. 나는 창을 가지고 있으며 프로그래밍 방식으로 NSTableView (NSScrollView)을 추가합니다. 지금까지 모든 것이 괜찮습니다. 내 창 크기를 조정할 수 있으며 테이블의 크기가 올바르게 조정됩니다. 이것은 내가 사용한 코드입니다.Cocoa Autolayout : 런타임에 프로그래밍 방식으로 뷰 삽입 (및 제거)

MyListViewController *viewController = [[MyListViewController alloc] initWithNibName:@"ListView" bundle:nil]; 

[self.mainContentView addSubview:viewController.view]; 
[viewController.view setTranslatesAutoresizingMaskIntoConstraints:NO]; 

[self.mainContentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[insertedView]|" 
                       options:0 
                       metrics:nil 
                        views:@{@"insertedView" :viewController.view}]]; 
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[insertedView]|" 
                      options:0 
                      metrics:nil 
                      views:@{@"insertedView" :viewController.view}]; 

[self.mainContentView addConstraints:verticalConstraints]; 

지금 .. 테이블의 맨 위에 배치 된 다른보기를 런타임에 추가 (제거)하고 싶습니다. 보기는 NSScrollViewNSPredicateEditor입니다. 명확하게 : 검색 필드에 무언가를 입력하고 가능한 검색 옵션이있는보기가 기본 창에 나타나면 Finder에서 동일한 동작을 얻으려고합니다.

나는이 방법으로이 문제를 해결할 :

AdvancedFilterViewController *viewController = [[AdvancedFilterViewController alloc] initWithNibName:@"AdvancedFilterView" bundle:nil]; 

[self.mainContentView addSubview:viewController.view]; 
[viewController.view setTranslatesAutoresizingMaskIntoConstraints:NO]; 

[self.mainContentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[insertedView]|" 
                      options:0 
                      metrics:nil 
                       views:@{@"insertedView" :viewController.view}]]; 
[self.mainContentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[insertedView(<=225)][prevView]|" // >= size of one row, <= size of 8 + 1 rows 
                      options:0 
                      metrics:nil 
                       views:@{@"insertedView" :viewController.view, @"prevView" : self.listView.view}]]; 

나는 술어 (25 점) 볼 수 있습니다 중 적어도 한 줄을 싶습니다. 사용자가 더하기 버튼을 누르면 더 많은 뷰가 나타나기 시작하고 스크롤 뷰는 크기를 조정해야합니다. 일부 행 (내가 결정 9) 후 스크롤바가 나타나기 시작하고 창에서 기본보기가 줄어들지 않아야합니다.

필터보기를 삽입 할 때 문제는 1 점과 거의 같은 크기입니다. 또는 25 포인트의 최소 크기를 추가하면보기가 표시되지만 행을 추가하면 스크롤보기의 '내부 크기'가 증가하므로 스크롤 막대가 표시됩니다 (그러나 스크롤보기가 커지지 않고 목록보기가 축소되지 않습니다) .

내가이 문제를 올바르게 설명했으면 좋겠다.

편집 : self.predicateEditorHeightNSScrollView의 높이에 제약 조건에 IBOutlet입니다

- (void)ruleEditorRowsDidChange:(NSNotification *)notification 
{ 
    //handle resizing of the view 
    [self.predicateEditorHeight setConstant:[self.predicateEditor numberOfRows] * self.predicateEditor.rowHeight]; 
} 

: 대답에서 영감을 촬영

나는 대리자를 구현했습니다. 그런 다음 NSWindowController을 묶을 때 술부 편집기 뷰를 추가 할 때 술어 뷰와 ​​관련하여 NSTableView의 비율에 제약 조건을 추가합니다. 이렇게하면 (0.3으로 설정했기 때문에) 스크롤 뷰는 특정 포인트까지 증가 할 수 있으며, 그 후에는 스크롤바를 사용해야합니다.

난 여전히 제약에 대한 몇 가지 문제가 : 나는 편집기에서 너무 많은 행을 추가 할 때 예를 들어 을, 나는 당신이 그 내부보기 것, 부모보기 '통보'에이

2014-04-26 09:38:27.769 MyApp[2531:303] Unable to simultaneously satisfy constraints: 
(
    "<NSLayoutConstraint:0x60000029dc40 V:[NSView:0x60000013fb80]-(0)-| (Names: '|':NSView:0x60000013e500)>", 
    "<NSLayoutConstraint:0x60000029dbf0 V:[NSView:0x600000320b40]-(0)-[NSView:0x60000013fb80]>", 
    "<NSLayoutConstraint:0x60000029dc90 NSView:0x60000013fb80.height >= 0.3*NSView:0x600000320b40.height>", 
    "<NSAutoresizingMaskLayoutConstraint:0x608000288b60 h=--& v=--& V:[NSView:0x60000013e500(360)]>", 
    "<NSLayoutConstraint:0x60000029dba0 V:|-(0)-[NSView:0x600000320b40] (Names: '|':NSView:0x60000013e500)>", 
    "<NSLayoutConstraint:0x60000029d1a0 V:[NSView:0x600000320f00(275)]>", 
    "<NSLayoutConstraint:0x60000029d420 V:[NSView:0x600000320f00]-(0)-| (Names: '|':NSView:0x600000320b40)>", 
    "<NSLayoutConstraint:0x60000029d510 V:[NSView:0x600000320dc0]-(0)-[NSView:0x600000320f00]>", 
    "<NSLayoutConstraint:0x60000029d330 V:|-(0)-[NSView:0x600000320dc0] (Names: '|':NSView:0x600000320b40)>", 
    "<NSLayoutConstraint:0x60000029d060 V:[NSButton:0x6000001569a0'Search']-(4)-| (Names: '|':NSView:0x600000320dc0)>", 
    "<NSLayoutConstraint:0x60000029d0b0 V:|-(5)-[NSButton:0x6000001569a0'Search'] (Names: '|':NSView:0x600000320dc0)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60000029d060 V:[NSButton:0x6000001569a0'Search']-(4)-| (Names: '|':NSView:0x600000320dc0)> 

2014-04-26 09:38:27.770 MyApp[2531:303] Unable to simultaneously satisfy constraints: 
(
    "<NSLayoutConstraint:0x60000029dc40 V:[NSView:0x60000013fb80]-(0)-| (Names: '|':NSView:0x60000013e500)>", 
    "<NSLayoutConstraint:0x60000029dbf0 V:[NSView:0x600000320b40]-(0)-[NSView:0x60000013fb80]>", 
    "<NSLayoutConstraint:0x60000029dc90 NSView:0x60000013fb80.height >= 0.3*NSView:0x600000320b40.height>", 
    "<NSAutoresizingMaskLayoutConstraint:0x608000288b60 h=--& v=--& V:[NSView:0x60000013e500(360)]>", 
    "<NSLayoutConstraint:0x60000029dba0 V:|-(0)-[NSView:0x600000320b40] (Names: '|':NSView:0x60000013e500)>", 
    "<NSLayoutConstraint:0x60000029d1a0 V:[NSView:0x600000320f00(275)]>", 
    "<NSLayoutConstraint:0x60000029d420 V:[NSView:0x600000320f00]-(0)-| (Names: '|':NSView:0x600000320b40)>", 
    "<NSLayoutConstraint:0x60000029d510 V:[NSView:0x600000320dc0]-(0)-[NSView:0x600000320f00]>", 
    "<NSLayoutConstraint:0x60000029d330 V:|-(0)-[NSView:0x600000320dc0] (Names: '|':NSView:0x600000320b40)>", 
    "<NSLayoutConstraint:0x60000029d100 V:|-(4)-[NSButton:0x6000001564d0] (Names: '|':NSView:0x600000320dc0)>", 
    "<NSLayoutConstraint:0x60000029d150 V:[NSButton:0x6000001564d0]-(5)-| (Names: '|':NSView:0x600000320dc0)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60000029d150 V:[NSButton:0x6000001564d0]-(5)-| (Names: '|':NSView:0x600000320dc0)> 

2014-04-26 09:38:27.775 MyApp[2531:303] Unable to simultaneously satisfy constraints: 
(
    "<NSLayoutConstraint:0x60000029cf70 V:[NSTextField:0x600000198940]-(4)-| (Names: '|':NSView:0x600000320dc0)>", 
    "<NSLayoutConstraint:0x60000029cfc0 V:|-(4)-[NSTextField:0x600000198940] (Names: '|':NSView:0x600000320dc0)>", 
    "<NSLayoutConstraint:0x60000029dc40 V:[NSView:0x60000013fb80]-(0)-| (Names: '|':NSView:0x60000013e500)>", 
    "<NSLayoutConstraint:0x60000029dbf0 V:[NSView:0x600000320b40]-(0)-[NSView:0x60000013fb80]>", 
    "<NSLayoutConstraint:0x60000029dc90 NSView:0x60000013fb80.height >= 0.3*NSView:0x600000320b40.height>", 
    "<NSAutoresizingMaskLayoutConstraint:0x608000288b60 h=--& v=--& V:[NSView:0x60000013e500(360)]>", 
    "<NSLayoutConstraint:0x60000029dba0 V:|-(0)-[NSView:0x600000320b40] (Names: '|':NSView:0x60000013e500)>", 
    "<NSLayoutConstraint:0x60000029d1a0 V:[NSView:0x600000320f00(275)]>", 
    "<NSLayoutConstraint:0x60000029d420 V:[NSView:0x600000320f00]-(0)-| (Names: '|':NSView:0x600000320b40)>", 
    "<NSLayoutConstraint:0x60000029d510 V:[NSView:0x600000320dc0]-(0)-[NSView:0x600000320f00]>", 
    "<NSLayoutConstraint:0x60000029d330 V:|-(0)-[NSView:0x600000320dc0] (Names: '|':NSView:0x600000320b40)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60000029cf70 V:[NSTextField:0x600000198940]-(4)-| (Names: '|':NSView:0x600000320dc0)> 
+0

AutoresizingMaskLayouts보기 - 모든 관련보기에서 setTranslatesAutoresizingMaskIntoConstraints를 NO로 설정 했습니까? – ThorstenC

+0

IB로 ​​설정할 수 있습니까? 아니면 프로그래밍 방식으로해야합니까? (그래서 귀하의 질문에 대한 대답은 아니오 :)) – Francesco

+1

IB와 프로그래밍 방식으로 설정할 수 있습니다. IB에서 뷰의 속성 관리자를 열고 '마스크를 제약 조건으로 변환'을 찾습니다. 코드에서 이미 뷰를 설정했습니다 : setTranslatesAutoresizingMaskIntoConstraints : NO]. 그러나 이것이 올바른 견해입니까? 상위 뷰 또는 조건부 뷰로 설정하십시오. – ThorstenC

답변

1

수 크기를 조정하십시오.

[[NSNotificationCenter defaultCenter]addObserverForName:@"RowsDidChange" 
    object:nil 
    queue:nil 
    usingBlock:^(NSNotification * notification){ 
     NSValue *value = (NSValue*)notification.object; 
     NSRect rectValue = [value rectValue]; 

     NSRect newContentRect = self.resultsView.frame; 
     newContentRect.size.height = self.resultsView.frame.size.height - rectValue.size.height; 
     newContentRect.size.width = self.resultsView.frame.size.width; 

     // Set new height to the mainViewController 
     [[self.mainViewController view] setFrame:newContentRect]; 
}]; 

당신은을 보낼 필요가 없습니다 사이즈의 변화를 볼 수

- (void)ruleEditorRowsDidChange:(NSNotification *)notification{ 

    NSInteger newRowCount = [_predicateEditor numberOfRows]; 
    NSInteger rowHeight = [_predicateEditor rowHeight]; 

    // Send a notification - size changes 
    NSRect newFrame = self.view.frame; 
    newFrame.size.height = 30 + newRowCount*rowHeight; 
    [[NSNotificationCenter defaultCenter]postNotificationName:@"RowsDidChange" object:[NSValue valueWithRect:newFrame]]; 
    [[_heightConstraint animator] setConstant:rowHeight*newRowCount]; 
} 

그리고 부모의 ViewController에서 '관찰자'나의 접근 방식은 술어보고 성장과 그 위임에 의해 축소했다 알림, 부모 viewcontroller 당신에게 어떤 대리자 메서드를 보낼 수도 있습니다.

행운을 빈다.

+0

감사합니다. 나는 거의 문제를 해결했다. 나는 아직도 제약에 관한 몇 가지 이슈를 가지고있다. 어쩌면 다른 힌트를 도울 수 있습니다 :) – Francesco

관련 문제