2015-06-13 30 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

相关问题