Stop firing keyboard when resetting view height

I am making iOS 8 login screen using Storyboard (Xcode6 beta7). Here's an image of the main idea:

enter image description here

Clicking inside any of the text boxes brings up the keyboard, and clicking on any of them cancels it again using:

self.view.endEditing(true)

      

Thus, pressing the login button causes the keyboard to disappear.

When the user enters invalid credentials, I want the dialog to grow in height and show some error message:

enter image description here

I am doing this with animation using the following function:

func increaseHeight(view: UIView, increment: CGFloat) {

    UIView.animateWithDuration(1.0,
        delay: 0.0,
        usingSpringWithDamping: 0.3,
        initialSpringVelocity: 3.0,
        options: UIViewAnimationOptions.CurveEaseInOut,
        animations: ({
            view.frame = CGRect(
                x: view.frame.origin.x,
                y: view.frame.origin.y,
                width: view.frame.width,
                height: view.frame.height + increment
            )
        }),
        completion: nil
    )
}

      

If I call this function, say by clicking the login button, everything works the way I want. The animation runs and an error message is displayed.

To the problem

However, if I first start editing any textbox (i.e. bringing the keyboard to the end) and then press the login button, the animation is done, but it reverts to the original height.

How can I make the height increment persist after the keyboard has been fired?

+3


source to share


1 answer


If you are using automatic layout, you need to change the constraints, not the view frame.

heightOfViewConstraint.constant += increment

UIView animation....{
    self.layoutIfNeeded()
}

      



This should keep your view stretched, but only if you are using auto layout and constraints.

0


source







All Articles