Search code examples
iosswiftanimationuitextfielduitextfielddelegate

Buttons moving after textField is clicked


I'm working on a login screen where you first choose whether you want to sign up through email, FB or Twitter. If you choose email, the buttons slide out of the screen, and the email login, with email text field, password text field and login button slide in the screen. I use this code:

@IBAction func emailLoginButtonPressed(sender: UIButton) {

    UIView.animateWithDuration(1, animations: { () -> Void in
        self.facebookButton.center = CGPointMake(self.facebookButton.center.x - 500, self.facebookButton.center.y)
        self.twitterButton.center = CGPointMake(self.twitterButton.center.x - 500, self.twitterButton.center.y)
        self.emailButton.center = CGPointMake(self.emailButton.center.x - 500, self.emailButton.center.y)
        self.emailTextField.center = CGPointMake(self.emailTextField.center.x - 500, self.emailTextField.center.y)
        self.passwordTextField.center = CGPointMake(self.passwordTextField.center.x - 500, self.passwordTextField.center.y)
        self.loginButton.center = CGPointMake(self.loginButton.center.x - 500, self.loginButton.center.y)

    })

}

This works perfectly: when the email login is chosen, everything slides left, and you can enter your email and password. However, as soon as I click in the email text field or the password text field, everything instantly comes back into place. Weirdly, if at this point I choose email login again, it still works and everything slides left. But overtime I click a text field, everything comes back as if I had just launched the app.

Any idea? I'm thinking it might have to do with UITextFieldDelegate doing something upon detecting clicks, but I don't know.

Thanks!


Solution

  • Add

            self.view.translatesAutoresizingMaskIntoConstraints = true
            self.emailTextField.translatesAutoresizingMaskIntoConstraints = true
            self.passwordTextField.translatesAutoresizingMaskIntoConstraints = true
            self.emailButton.translatesAutoresizingMaskIntoConstraints = true
            self.facebookButton.translatesAutoresizingMaskIntoConstraints = true
            self.twitterButton.translatesAutoresizingMaskIntoConstraints = true
            self.loginButton.translatesAutoresizingMaskIntoConstraints = true
    

    to your view controller's viewDidLoad