我有一个观点。在该视图中,我在代码中生成了许多子视图。我希望每个子视图都伸展到封闭视图的宽度。我尝试在代码中添加约束但是我得到了运行时异常。
NSView *trackcontainer = [self trackContainerWithTrack:track];
[self addSubview:trackcontainer];
[trackcontainer addConstraints: [NSLayoutConstraint
constraintsWithVisualFormat:@"|-5-[trackcontainer]-5-|"
options:0 metrics:nil
views:NSDictionaryOfVariableBindings(trackcontainer)]];
例外是:
2014-05-12 16:58:20.812 testApp[7597:303] Unable to install constraint on view.
Does the constraint reference something from outside the subtree of the view?
That's illegal. constraint:<NSLayoutConstraint:0x60000028e880 H:|-(5)-[BTSDoubleClickView:0x600000340fd0]
(Names: '|':BTSAlbumTracklistView:0x608000137c00 )> view:<BTSDoubleClickView: 0x600000340fd0>
嗯,当然,它不在视野之内。我希望它能引用超视图,这就是我使用管道字符的原因。
编辑:我刚刚意识到我应该在superview中添加约束,而不是子视图。我做到了,但现在我得到了一个不同的错误:2014-05-12 22:37:47.218 autolayouttest[29399:303] Unable to simultaneously satisfy constraints:
(
"<NSAutoresizingMaskLayoutConstraint:0x61800009e730 h=--& v=--& H:|-(10)-[NSView:0x60000013aa40] (Names: '|':FunkyView:0x60800013aa40 )>",
"<NSLayoutConstraint:0x60000009eaa0 H:|-(NSSpace(20))-[NSView:0x60000013aa40] (Names: '|':FunkyView:0x60800013aa40 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60000009eaa0 H:|-(NSSpace(20))-[NSView:0x60000013aa40] (Names: '|':FunkyView:0x60800013aa40 )>
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.
我知道存在冲突,可能是因为我在创建视图框架时指定了宽度/高度?我真的不知道从哪里开始。我需要添加多少约束?我可以删除AutoresizingMaskLayoutConstraint吗?我在视图上没有任何自动调整遮罩(我尝试将其设置为0以防万一,没有区别。)
答案 0 :(得分:0)
当使用自动布局代替AutoresizingMasks时,您需要在视图上调用setTranslatesAutoresizingMaskIntoConstraints:NO
。