I have a UILabel outlet that has its top space to superview and leading space to superview set to 20 in storyboard. I want to press a button to dynamically change the constraints to be centered vertically and horizontally. The vertical dynamic change is working fine, but the horizontal dynamic change isn't changing anything:
@IBAction func moveLabel(sender: AnyObject) {
self.view.addConstraint(NSLayoutConstraint(
item: self.label,
attribute: NSLayoutAttribute.CenterY,
relatedBy: NSLayoutRelation.Equal,
toItem: self.view,
attribute: NSLayoutAttribute.CenterY,
multiplier: 1,
constant: 0))
self.view.addConstraint(NSLayoutConstraint(
item: self.label,
attribute: NSLayoutAttribute.CenterX,
relatedBy: NSLayoutRelation.Equal,
toItem: self.view,
attribute: NSLayoutAttribute.CenterX,
multiplier: 1,
constant: 0))
}
The horizontal spacing stays at 20 leading from superview, but the vertical spacing is correctly applied to be centered. Why isn't the horizontal center working?