2015-02-11 194 views
0

我有一個NSView讓我打電話詳細視圖在我的主視圖控制器中,所有的自動佈局約束將被添加到與主視圖相關。我將添加一個子視圖讓這個子細節到這個細節視圖基於用戶輸入。根據子細節視圖,此子細節視圖包含大量視圖,其中添加了約束。在添加子細節細節視圖作爲子視圖我通過獲取細節視圖的框架並設置x = 0和y = 0;來設置框架。我的問題是,當用戶最大化窗口,用戶選擇和我加入子細節詳細查看子詳細的尺寸子視圖保持相同,但子詳細觀點得到自動調整爲每需要。任何人都可以指導我做錯了什麼。下面我分享你的代碼,其中我在鏈接中添加子視圖和輸出視圖 enter image description here 我不想要空白的藍色空間。如何根據自動佈局調整子視圖子視圖的大小?

NSRect f = detailView.frame; 
f.origin.x = 0; 
f.origin.y = 0; 
ConfigLogin *subDetail = [[ConfigLogin alloc] initWithFrame:f]; 
[subDetail setWantsLayer:YES]; 
[subDetail setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable]; 
[subDetail setTranslatesAutoresizingMaskIntoConstraints:YES]; 
subDetail.layer.backgroundColor = [NSColor blueColor].CGColor; 
subDetail.frame = f; 
[detailView addSubview:detailView]; 

回答

3

我能找到的同時加入了子視圖,我們不加約束,以使視圖適合超級查看答案。

+(void) fitSubViewToSuperView:(NSView *) subview superView:(NSView *) superView 
    { 
NSLayoutConstraint *width = [NSLayoutConstraint 
           constraintWithItem:subview 
           attribute:NSLayoutAttributeWidth 
           relatedBy:0 
           toItem:superView 
           attribute:NSLayoutAttributeWidth 
           multiplier:1.0 
           constant:0]; 

NSLayoutConstraint *height = [NSLayoutConstraint 
           constraintWithItem:subview 
           attribute:NSLayoutAttributeHeight 
           relatedBy:0 
           toItem:superView 
           attribute:NSLayoutAttributeHeight 
           multiplier:1.0 
           constant:0]; 

NSLayoutConstraint *top  = [NSLayoutConstraint 
           constraintWithItem:subview 
           attribute:NSLayoutAttributeTop 
           relatedBy:NSLayoutRelationEqual 
           toItem:superView 
           attribute:NSLayoutAttributeTop 
           multiplier:1.0f 
           constant:0.f]; 

NSLayoutConstraint *leading = [NSLayoutConstraint 
           constraintWithItem:subview 
           attribute:NSLayoutAttributeLeading 
           relatedBy:NSLayoutRelationEqual 
           toItem:superView 
           attribute:NSLayoutAttributeLeading 
           multiplier:1.0f 
           constant:0.f]; 
[superView addConstraint:width]; 
[superView addConstraint:height]; 
[superView addConstraint:top]; 
[superView addConstraint:leading]; 
} 
相關問題