ajrlewis
ajrlewis

Reputation: 3058

Programatically adding NSConstraints to existing views on storyboards

i'm finding that constraints aren't implemented correcting when typed programatically to existing views on story boards. here's what i have:

class ViewController: UIViewController {

    @IBOutlet weak var textField2: UITextField! {
        didSet {
            textField2.delegate = self
        }
    }

    func viewDidLoad() {
        super.viewDidLoad()

        let superview = self.view

        let textField1 = UITextField()
        textField1.backgroundColor = UIColor.greenColor()
        textField1.placeholder = "test text field"
        view.addSubview(textField1)
        textField1.translatesAutoresizingMaskIntoConstraints = false
        superview.addConstraint(NSLayoutConstraint(item: textField1, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: superview.frame.width * 0.50))
        superview.addConstraint(NSLayoutConstraint(item: textField1, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1, constant: superview.frame.height * 0.05))
        superview.addConstraint(NSLayoutConstraint(item: textField1, attribute: .CenterX, relatedBy: .Equal, toItem: superview, attribute: .CenterX, multiplier: 1, constant: 0))
        superview.addConstraint(NSLayoutConstraint(item: textField1, attribute: .CenterY, relatedBy: .Equal, toItem: superview, attribute: .CenterY, multiplier: 1, constant: 0))

        textField2.translatesAutoresizingMaskIntoConstraints = false
        superview.addConstraint(NSLayoutConstraint(item: textField2, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: superview.frame.width * 0.50))
        superview.addConstraint(NSLayoutConstraint(item: textField2, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1, constant: superview.frame.height * 0.05))
        superview.addConstraint(NSLayoutConstraint(item: textField2, attribute: .CenterX, relatedBy: .Equal, toItem: superview, attribute: .CenterX, multiplier: 1, constant: 0))
        superview.addConstraint(NSLayoutConstraint(item: textField2, attribute: .CenterY, relatedBy: .Equal, toItem: superview, attribute: .CenterY, multiplier: 1, constant: 0))

    }
}

textField1 works perfectly. it is add to the centre of the view. however, textField2, which i dragged and dropped onto the storyboard, is offset from the centre. why is this happening?

thanks

Upvotes: 0

Views: 40

Answers (1)

matt
matt

Reputation: 535556

The problem is that if a view comes from the storyboard, like your textField2, it already has constraints. Saying textField2.translatesAutoresizingMaskIntoConstraints = false does not magically solve that problem. You would also need to get hold of those constraints, somehow, and remove them before applying your own constraints in code. That can be tricky to do, and you are not doing it, which is why your view is misbehaving. It is better, at this stage, for you to follow these simple rules:

  • if a view comes from the storyboard, set its constraints only in the storyboard

  • but if a view is made in code, now you can say translatesAutoresizingMaskIntoConstraints = false and set its constraints in code

Upvotes: 1

Related Questions