如何重写视觉格式
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[label]-|", options: .AlignAllBaseline, metrics: nil, views: ["label": label]))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[label]-|", options: .AlignAllCenterX, metrics: nil, views: ["label": label]))
通过移动到布局指南(带边距)?
我尝试过
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
after super
被执行)。设置了约束,但表现得就像零裕度一样。它布局并给出预期的layoutFrame
仅当布局边距设置为负数时;这显然不是我想要的,但表明约束是通过边距指南设置的。看来我错过了一些东西......
在我看来,layoutMarginsGuide 在 UIView 的 init 方法中还没有准备好。我也遇到类似的问题,在 updateConstraints 中设置约束就可以了。仍在调查原因。
更新:我想做的是在将视图添加到超级视图(在 init 中)之前设置layoutMargins。那行不通。有效的是在 init 中设置相对于layoutMarginsGuide 的约束,但实际上是在 viewDidMoveToSuperView 中设置layoutMargins。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)