0
votes
    signUpButton.translatesAutoresizingMaskIntoConstraints = false
    signUpButton.widthAnchor.constraint(equalToConstant: 128).isActive = true
    signUpButton.heightAnchor.constraint(equalToConstant: 64).isActive = true
    signUpButton.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    signUpButton.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    signUpButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
  • NSLayoutConstraint: UIButton:'Sign Up'.width == 128 (active),
  • NSLayoutConstraint: H:|-(0)-[UIButton:'Sign Up'] (active, names: '|':UIView: ),
  • NSLayoutConstraint: UIButton:'Sign Up'.trailing == UIView:.trailing (active),
  • NSLayoutConstraint: 'UIView-Encapsulated-Layout-Width' UIView:.width == 375 (active)

Will attempt to recover by breaking constraint NSLayoutConstraint: UIButton:'Sign Up'.width == 128 (active)>

I want to put my button to center of leading and trailing anchors with certain width but when I run the code the button expands to those anchors. How can I prevent that expansion?

1
Unrelated but consider replacing the individual uses of .isActive = true with the user of NSLayoutContraint.activate().rmaddy

1 Answers

0
votes

Since it seems you wish to center the button horizontally within view, you need to replace:

signUpButton.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
signUpButton.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

with:

signUpButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true