Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build an Interactive Story App with Swift 2 Adding Sound Effects Repositioning the Text Field

Reed Carson
Reed Carson
8,306 Points

Duration Value in animateWithDuration Method is Irrelevant

UIView.animateWithDuration(0.8) { self.textFieldBottomConstrain.constant = keyboardFrame.size.height + 10

I noticed when we use this method that changing the value for duration has no effect on the show and hid animation. Even setting it to 5 seconds up from 0.8 makes no difference. Whats going on?

1 Answer

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

You are right, animating constraints with the UIView animation methods does not work this way. Your constraint is not animated whatsoever. The following should work:

// Set the constant value *before* calling the animation method
self.textFieldBottomConstrain.constant = keyboardFrame.size.height + 10

UIView.animateWithDuration(0.8) {
   // Calling layoutIfNeeded on the view containing the view
   // you want to animate will trigger the animation
   self.view.layoutIfNeeded()
}

Useful links on that topic:

Hope that helps :)

P.S. Take care, getting the correct height of the keyboard is a tricky one! Let's google this, I am sure you will find some good explanations on it!

Reed Carson
Reed Carson
8,306 Points

Cool thanks. This should be useful to know. Im wondering however why this way wasn't shown in the video instead? Or did I just mix it up.