自动布局:layoutMarginsGuide [英] Auto Layout: layoutMarginsGuide
问题描述
如何重写视觉格式
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[label]-|", options: .AlignAllBaseline, metrics: nil, views: ["label": label]))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[label]-|", options: .AlignAllCenterX, metrics: nil, views: ["label": label]))
转移到布局指南(有边距)?
by moving to layout guides (with margins)?
我尝试了
label.topAnchor.constraintEqualToAnchor(layoutMarginsGuide.topAnchor).active = true
label.leftAnchor.constraintEqualToAnchor(layoutMarginsGuide.leftAnchor).active = true
label.bottomAnchor.constraintEqualToAnchor(layoutMarginsGuide.bottomAnchor).active = true
label.rightAnchor.constraintEqualToAnchor(layoutMarginsGuide.rightAnchor).active = true
但不起作用。即使 layoutMarginsGuide.layoutFrame
也没有预期值(是的,我在之后在
超级
之后称之为
已执行)。设置了约束,但行为就像没有余量一样。只有当布局边距设置为负时,它才会布局并给出预期的 layoutSubviews
layoutFrame
;这显然不是我想要的,但证明了约束是用边距指南设置的。看起来我错过了什么......
but does not work. Even layoutMarginsGuide.layoutFrame
does not have expected value (yes I call it in layoutSubviews
after super
is executed). Constraints are set, but acts like there is zero margin. It layouts and gives expected layoutFrame
only when the layout margin is set to negative; which is not what I want obviously, but demonstrates that constraints are set with margins guides. Looks like I'm missing something...
推荐答案
在我看来,layoutMarginsGuide还没有准备好在init方法中UIView的。我也遇到了类似的问题,在updateConstraints中设置约束只是起作用。仍在调查原因。
It seems to me that layoutMarginsGuide is not ready in the init method of a UIView. I'm also getting similar issues, where setting up the constraints in updateConstraints just works. Still investigating why.
更新:我想要做的是在将视图添加到superview(在init中)之前设置layoutMargins。那没用。相对于layoutMarginsGuide,在init中设置约束的工作是什么,但实际上在viewDidMoveToSuperView中设置layoutMargins。
UPDATE: What I was trying to do was set the layoutMargins before the view had been added to a superview (in the init). That didn't work. What did work was setting the constraints in the init, relative to layoutMarginsGuide, but actually setting the layoutMargins in viewDidMoveToSuperView.
这篇关于自动布局:layoutMarginsGuide的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!