我要做的就是添加一个NSImageView,并在每次调整大小时使其填充Window。
但是,一旦添加了约束,窗口就会失去其调整大小的能力。
如果我加
view.translatesAutoresizingMaskIntoConstraints = false
约束也被忽略。
有人可以指出我做错了什么吗?
可以从这里下载代码
首先,您parentView
是窗口的contentView
。我想不到有一次会干扰该视图的默认布局行为,因此我将删除该行parentView.translatesAutoresizingMaskIntoContraints = false
。
Secondly, by the looks of things you do want to take full responsibility for constraining the image view, so you for this view you should set translatesAutoresizingMaskIntoContraints
to false.
Finally, in addition to the constraints you add, all NSView
objects come with constraints that tell Auto Layout to what extent the view's content should effect how and whether it resizes. What's meant by content varies depending on the view, but in your case it's the NSImage
object. In your code you're getting the default behaviour, which basically says don't allow any content compression - you need to change this:
imageView.setContentCompressionResistancePriority(249, forOrientation: NSLayoutConstraintOrientation.Horizontal)
imageView.setContentCompressionResistancePriority(249, forOrientation: NSLayoutConstraintOrientation.Vertical)
确实,除非您有令人信服的理由,否则应该NSImageView
在Interface Builder中进行设置。Interface Builder不仅消除了行和行代码,还提供了实时调试帮助。例如,我通过注释掉所有代码并首先在Interface Builder中重做整个问题来回答了这个问题。当我选择图像视图时,就会出现橙色的虚线警告线,这是一个很明显的问题,即压缩问题,这很明显:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句