0

I am making a sign up screen and would like to update a few top anchors so that when the keyboard appears, the top anchor constant decreases and the keyboard doesn't cover any text fields.

I have created a topConstant variable:

var constraintConstant: CGFloat = 35

And have set up my views as follows:

view.addSubview(passwordTextField)
passwordTextField.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 25).isActive = true
passwordTextField.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -25).isActive = true
passwordTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true
passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: constraintConstant).isActive = true

Then I have written this code:

func textFieldDidBeginEditing(_ textField: UITextField) {
    constraintConstant = 15
    view.layoutIfNeeded()
}

I'n not sure why the constriants aren't updating. Any ideas?

2 Answers 2

0

The constant that you specify doesn't function like how you are expecting it to. You are expecting it to function like some kind of listener which will keep updating if the value of the variable is updated. It doesn't. It will just take the value of the variable at the time of setting and then not look back unless you access that constraint and change the constant manually.

Which is why you have to store the instance of the constraint and change the constant maually.

Define the constraint variable:

var topAnchorConstraint: NSLayoutConstraint!

Store the appropriate constraint in the variable

topAnchorConstraint = passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: 35)
topAnchorConstraint.isActive = true

Now you need to change the constant as required.

func textFieldDidBeginEditing(_ textField: UITextField) {
    UIView.animate(withDuration: 1.0, animations: {
        self.topAnchorConstraint.constant = 15
        self.view.layoutIfNeeded()

    }, completion: nil)
}
Sign up to request clarification or add additional context in comments.

5 Comments

Worked like a charm. Thanks!
@Rakesha Shastri isn't this a duplicate
@Sh_Khan no, yours doesn’t seem to have any explanation whatsoever.
@RakeshaShastri i don't need the right check , i liked to remind you of site policy at least in the scope of one answer not other duplicates
@Sh_Khan site policy clearly says “exact duplicates”, which mine isn’t. Also if wanted to follow site policy, you’d have put a little more effort into your answer.
0

You need

var topCon:NSLayoutConstraint!

//

topCon = passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: constraintConstant)
topCon.isActive = true

//

func textFieldDidBeginEditing(_ textField: UITextField) {
    topCon.constant = 15
    view.layoutIfNeeded()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.