以编程方式将视图添加到 NSStackView 后,视图无法正确显示
Views not displaying correctly after adding them to NSStackView programmatically
我正在尝试向 NSStackView 添加一些视图(本例中为 NSTextField):
我已将 NSStackView 实例添加到 XIB 文件中,并将其作为 widgets 链接到 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]);
TextFields 被放置在 StackView 中,但它们被放置在相同的位置,即第二个完全重叠。我仍然可以 select 先按 [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及结果截图:
StackView 没有添加任何限制。 StackView 应该是垂直的。将有动态数量的文本字段添加到堆栈视图。
您需要将文本字段的 translatesAutoresizingMaskIntoConstraints
属性 设置为 false。
通常情况下,如果视图是由其他代码放置的,则不应执行此操作。也就是说,您会期望堆栈视图决定 属性 应该打开还是关闭,因为它控制着将它放入视图层次结构中。但是,在 10.10 之前,NSStackView
中存在与此相关的错误,因此您必须自行将其关闭。
线索是不可满足的约束集包括 NSAutoresizingMaskLayoutConstraint
.
类型的约束
我正在尝试向 NSStackView 添加一些视图(本例中为 NSTextField):
我已将 NSStackView 实例添加到 XIB 文件中,并将其作为 widgets 链接到 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]);
TextFields 被放置在 StackView 中,但它们被放置在相同的位置,即第二个完全重叠。我仍然可以 select 先按 [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及结果截图:
StackView 没有添加任何限制。 StackView 应该是垂直的。将有动态数量的文本字段添加到堆栈视图。
您需要将文本字段的 translatesAutoresizingMaskIntoConstraints
属性 设置为 false。
通常情况下,如果视图是由其他代码放置的,则不应执行此操作。也就是说,您会期望堆栈视图决定 属性 应该打开还是关闭,因为它控制着将它放入视图层次结构中。但是,在 10.10 之前,NSStackView
中存在与此相关的错误,因此您必须自行将其关闭。
线索是不可满足的约束集包括 NSAutoresizingMaskLayoutConstraint
.