0


I have the most strange situation with a custom keyboard.
First of all, I have set up a dummy view for the textfield, in order to hide the stock keyboard let dummyView : UIView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1)) amountField.inputView = dummyView
Then I have my custom keyboard which animates when editing begins in the text field

func textFieldDidBeginEditing(textField: UITextField) {
        keyboardContainer.hidden = false
        UIView.animateWithDuration(0.6, animations: {
            self.keyboardContainer.frame = self.keyboardScreenPosition!
            }, completion: {
                finished in
                if finished {
                    //just in case
                }
        })

    }


Also, I have set up a button which should end editing and hide my custom keyboard

@IBAction func calculeaza(sender: AnyObject) {

        self.amountField.resignFirstResponder()

        UIView.animateWithDuration(0.6, animations: {

            self.keyboardContainer.frame.origin.y = self.view.bounds.height
            }, completion: {
                finished in
                if finished {

                }
        })
    }


The most strange part comes with the resignFirstResponder(). Let me explain: If that part is not included, the keyboard hides just fine (but the text field keeps on blinking cursor which is not an option ofc).
If the resigning part is included, the keyboard animates from the top to its current position, then on pressing the button again it does slides down as intended.
I am really puzzled on why is this happening...I debugged the sizes of the view and the heights are ok so it should slide down from the beginning. I really dont understand what is happening.
Any help is much appreciated, many thanks!

EDIT: another strange effect is if I move the resign part (or the super end editing) in the animation ending closure. The keyboard slides just fine, then it reappears on screen

Kevin
  • 16,696
  • 7
  • 51
  • 68
Razvan Soneriu
  • 587
  • 3
  • 7
  • 23

1 Answers1

0

This sounds like an issue with autolayout constraints. Those are updated after the animateWithDuration which is potentially causing this odd behavior. If you have (a) constraint(s) in the Storyboard used for autolayout, try updating that(/those). If you haven't already, you'll need to add it as an IBOutlet and animate the autolayout change in the duration.

For example, say the constraint is called theRelevantConstraint. Then replace the middle lines

self.theRelevantConstraint.constant = valueItShouldBe
UIView.animateWithDuration(0.6, animations: {
  self.view.layoutIfNeeded()
  }, completion: {
    finished in
    if finished {

    } 
})
roc
  • 2,829
  • 1
  • 11
  • 6