2015-06-13 40 views
2

所以,我想補充一些意見(的NSTextField在這個例子中)到NSStackView:意見不將它們添加到NSStackView後正確顯示編程

我添加NSStackView例如到XIB文件,鏈接它出口as widgets into Document。在...... didLoadNib我這樣做:

NSTextField *tf = [[NSTextField alloc] init]; 
tf.stringValue = @"124"; 
[tf setFrameSize:NSMakeSize(100, 20)]; 
[widgets addView:tf inGravity:NSStackViewGravityLeading]; 
NSLog(@"%f - %d", NSHeight(tf.frame), [tf hasAmbiguousLayout]); 

NSTextField *tf2 = [[NSTextField alloc] init]; 
tf2.stringValue = @"123"; 
[tf2 setFrameSize:NSMakeSize(100, 20)]; 
[widgets addView:tf2 inGravity:NSStackViewGravityLeading]; 
NSLog(@"%f - %d", NSHeight(tf2.frame), [tf2 hasAmbiguousLayout]); 

的TextField放入StackView,但它們放置到同一位置,即第二首完全重疊。我仍然可以通過[space + tab]首先選擇。

這裏是控制檯輸出:

2015-06-13 17:11:00.736 celty-test[62306:9217679] 20.000000 - 1 
2015-06-13 17:11:00.739 celty-test[62306:9217679] Unable to simultaneously satisfy constraints: 
(
    "<NSLayoutConstraint:0x608000084bf0 V:[NSStackViewSpacer:0x6080001834d0(>=8)]>", 
    "<NSLayoutConstraint:0x608000084f10 V:[NSTextField:0x608000183330]-(0)-[NSStackViewSpacer:0x6080001834d0]>", 
    "<NSLayoutConstraint:0x608000085460 V:[NSStackViewSpacer:0x6080001834d0]-(0)-[NSTextField:0x608000183400]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x608000084970 h=--& v=--& V:[NSTextField:0x608000183330]-(0)-| (Names: '|':NSStackViewContainer:0x6000001a0700)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x608000084d30 h=--& v=--& V:[NSTextField:0x608000183400]-(0)-| (Names: '|':NSStackViewContainer:0x6000001a0700)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x608000084d80 h=--& v=--& V:[NSTextField:0x608000183400(20)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x608000084bf0 V:[NSStackViewSpacer:0x6080001834d0(>=8)]> 

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. 
2015-06-13 17:11:00.802 celty-test[62306:9217679] Unable to simultaneously satisfy constraints: 
(
    "<NSLayoutConstraint:0x608000084f10 V:[NSTextField:0x608000183330]-(0)-[NSStackViewSpacer:0x6080001834d0]>", 
    "<NSLayoutConstraint:0x608000085460 V:[NSStackViewSpacer:0x6080001834d0]-(0)-[NSTextField:0x608000183400]>", 
    "<NSAutoresizingMaskLayoutConstraint:0x608000084970 h=--& v=--& V:[NSTextField:0x608000183330]-(0)-| (Names: '|':NSStackViewContainer:0x6000001a0700)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x608000084d30 h=--& v=--& V:[NSTextField:0x608000183400]-(0)-| (Names: '|':NSStackViewContainer:0x6000001a0700)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x608000084d80 h=--& v=--& V:[NSTextField:0x608000183400(20)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x608000085460 V:[NSStackViewSpacer:0x6080001834d0]-(0)-[NSTextField:0x608000183400]> 

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. 
2015-06-13 17:11:00.814 celty-test[62306:9217679] 20.000000 - 0 

XIB和結果截圖: XIB

result

有沒有加入到StackView約束。 StackView應該是垂直的。將有大量的文本框被添加到stackview中。

+0

小工具有限制嗎?你有引力將兩個文本字段設置到小部件的底部,從調試器看起來像你在文本字段之間添加了間距,但是如果你想要一個文本字段,它們需要被固定到小部件的頂部可調整大小的部件像'「V:| [tf] -8- [tf2] |」' – Tim

+0

我沒有添加到ScrollView或textfields中。 – ShadowPrince

回答

8

您需要將文本字段的translatesAutoresizingMaskIntoConstraints屬性設置爲false。

通常情況下,如果視圖被其他代碼放置,則不應該這樣做。也就是說,您會希望堆棧視圖決定該屬性是否應該打開或關閉,因爲它控制着將其放入視圖層次結構中。然而,從這個到10.10,有一個NSStackView的錯誤,所以你必須自己關閉它。

線索是一套不可滿足的限制包括類型NSAutoresizingMaskLayoutConstraint

+0

謝謝你。只有第二天進入可可並已經發現了一個錯誤。 – ShadowPrince

+0

它看起來像是在10.11上修復的,'NSStackView'設置'translatesAutoresizingMaskIntoConstraints'爲添加的視圖爲false。 – Taylor

+0

非常感謝!我剛花了幾個小時試圖讓這個工作,這固定它。奇怪的是,我有另一個堆棧視圖與相同的設置,它可以正常工作,而不會這樣做... – DanielGibbs

相關問題