Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField, when resigning first responder, causing strange animation scroll of text

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.

like image 278
brandonscript Avatar asked Jan 31 '16 06:01

brandonscript


2 Answers

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()
    }
}
like image 196
brandonscript Avatar answered Oct 12 '22 12:10

brandonscript


Try this

- (void)textFieldDidEndEditing:(UITextField *)textField
{
  [textField layoutIfNeeded];
}
like image 29
Jim75 Avatar answered Oct 12 '22 12:10

Jim75