9

I've got a couple of UITextFields implemented in a UITableView for a login form. When resigning first responder in both the very first time, a really strange animation jump is occurring. Since these are almost entirely build in Interface Builder with a .xib file, I've got virtually no code to add in. But here's a fun .gif that shows the behavior:

Update:

I've narrowed it down to the fact that I'm listening to keyboard events to adjust the view constraints. This is the code that's causing the problem:

func keyboardWillHide(notification: NSNotification) {
    // tried self.formContainer.layoutIfNeeded() here too to force pending layouts
    formContainerYConstraint.constant = 40
    UIView.animateWithDuration(0.4) { () -> Void in
        self.formContainer.layoutIfNeeded()
    }
}

... where the form container is a view that houses the table view and login button.

brandonscript
  • 68,675
  • 32
  • 163
  • 220

2 Answers2

4

Feels like a total hack (and I'd love for someone to post a better answer) but in the mean time, I've resolved this by adding a slight delay to the animation action - I suspect this is related to the become- and resignFirstResponder events occurring when switching between two input fields.

let delay: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
dispatch_after(delay, dispatch_get_main_queue()) { () -> Void in
    self.formContainerYConstraint.constant = 40
    UIView.animateWithDuration(0.4) { () -> Void in
        self.formContainer.layoutIfNeeded()
    }
}
brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • Was about to post the exact same question with the exact same hacky solution. I haven't been able to find anything better either. – mattsson Feb 03 '16 at 08:55
  • 1
    Interestingly for me before I applied the above code the animation duration was not respected and seemed to automatically sync with the keyboard animation. After adding dispatch_after I had to tweak the animation duration to try and match what the keyboard was doing – Doug Amos Jul 04 '16 at 10:26
  • Yeah it's an ugly hack for sure – brandonscript Jul 04 '16 at 15:03
1

Try this

- (void)textFieldDidEndEditing:(UITextField *)textField
{
  [textField layoutIfNeeded];
}
Jim75
  • 737
  • 8
  • 13
  • as suggested here:http://stackoverflow.com/questions/33544054/why-is-uitextfield-animating-on-resignfirstresponder/35407734#35407734 – Jim75 May 05 '17 at 15:39