0

Here is my problem, I want to animate my login screen which is a basic screen, 2 UITextView and 2 buttons (Log in and Signup)

I want to make my Username field appear out of the screen when the app launches and animate it , so here is my code :

override func viewWillAppear(animated: Bool) {
    self.navigationController?.navigationBarHidden = true
    print (usernameField.center.x)

    usernameField.center.x -= view.bounds.width
    print (view.bounds.width)
    print (usernameField.center.x)

}

Here is the output :

300.0
375.0
-75.0

So the final x of my usernameField is -75.0, but in all cases it is centered in my app.

I think it's because I applied constraints to it in my Storyboard ? (Suggested constraints which center buttons and textview)

Thanks for your help

EDIT : Removing constraints didn't resolve my problem ..

Nicolas Charvoz
  • 1,509
  • 15
  • 41
  • try to do all frame related calculations in viewDidLayoutSubviews. Btw if you use constraints don't try to change view's frame cause this changes are ignored or generate unexpected behaviour. – nsinvocation Mar 08 '16 at 09:03

1 Answers1

1

You will need to keep reference to the constraint that maintains the leading space with the immediate neighbour/margin. Then set appropriate values to the constant property of this constraint. And then to animate, you will need to call the layoutIfNeeded method in an animation block.

Initially ensure, your view is out of bounds and towards the left of the view.

Have an outlet for the constraint say-

@IBOutlet weak var constraint : NSLayoutConstraint!

override func animateIn(){
  //Do any pending layout
  self.view.layoutIfNeeded()
  constraint.constant += 100 //<-- set appropriate value for your case
  //Animate the constraint change to bring in the button into the screen

  UIView.animateWithDuration(0.2, animations: {
    self.view.layoutIfNeeded()
})

}

Refer: How do I animate constraint changes?

Community
  • 1
  • 1
Shripada
  • 6,296
  • 1
  • 30
  • 30