如何重写视觉格式
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[label]-|", options: .AlignAllBaseline, metrics: nil, views: ["label": label]))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[label]-|", options: .AlignAllCenterX, metrics: nil, views: ["label": label]))
Run Code Online (Sandbox Code Playgroud)
通过移动到布局指南(带边距)?
我试过了
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
Run Code Online (Sandbox Code Playgroud)
但不起作用.甚至layoutMarginsGuide.layoutFrame没有预期的价值(是的,我在执行layoutSubviews后称之为super).设置了约束,但行为就像零保证金一样.layoutFrame只有当布局边距设置为负时,才会布局并给出预期; 这显然不是我想要的,但表明约束是用边距指南设置的.看起来我错过了一些东西......