为什么添加约束会消除调整NSWindow的能力?

时间:2016-08-18 08:10:27

标签: cocoa nsview nslayoutconstraint nswindow nsautolayout

enter image description here

我正在尝试的是,每当我调整大小时,添加NSImageView并使其填充窗口。

但是,一旦添加了约束,窗口就会失去调整大小的能力。

enter image description here

如果我添加

rect

也忽略了约束。

enter image description here

有人可以指出我在哪里做错了吗?

可以从here

下载代码

1 个答案:

答案 0 :(得分:3)

首先,您的"LaunchConfig" : { "Type" : "AWS::AutoScaling::LaunchConfiguration" "Metadata" : { "AWS::CloudFormation::Init" : { "configSets" : { "InstallAndRun" : [ "config1" ] }, "config1" : { "commands" : { "test" : { "command" : "echo \"$MAGIC\" > test.txt", "env" : { "MAGIC" : "I come from the environment!" }, "cwd" : "~", "test" : "test ! -e ~/test.txt", "ignoreErrors" : "false" } } } } }, "Properties" : { ... } } 是窗口parentView。我无法想到我曾干扰此视图的默认布局行为,因此我删除了行contentView

其次,根据您的事情,您希望对约束图片视图承担全部责任,因此,对于此视图,您应该设置parentView.translatesAutoresizingMaskIntoContraints = false为假。

最后,除了您添加的约束之外,all NSView objects come with constraints that tell Auto Layout to what extent the view's content should effect how and whether it resizes。内容的含义因视图而异,但在您的情况下,它是translatesAutoresizingMaskIntoContraints对象。在您的代码中,您获得默认行为,基本上表示不允许任何内容压缩 - 您需要更改此内容:

NSImage

实际上,除非您有令人信服的理由,否则您应该在Interface Builder中设置imageView.setContentCompressionResistancePriority(249, forOrientation: NSLayoutConstraintOrientation.Horizontal) imageView.setContentCompressionResistancePriority(249, forOrientation: NSLayoutConstraintOrientation.Vertical) 。 Interface Builder不仅省去了行和行代码,还提供了实时调试帮助。例如,我通过注释掉所有代码并首先在Interface Builder中重做整个问题来回答这个问题。事实上,当我选择图像视图时出现的是虚线橙色警告线,这个问题是压缩约束的事情是显而易见的:

enter image description here