I have a UIView
which contains another UIView
object called contentView
. Inside contentView
, I have several UITextField
objects. So that the current editing UITextField
is always visible and not being hidden by the keyboard, I am altering the constraints on the contentView
inside the textFieldDidBeginEditing:
method. This means the contentView slides up and down inside the parent UIView
, and keeps the relevant UITextField
visible. This part is working fine, but here is a code fragment:
- (void)textFieldDidBeginEditing:(UITextField *)textField
//offset calculation removed for clarity
NSInteger offset = ....
self.contentViewTopConstraint.constant = -offset;
self.contentViewBottomConstraint.constant = offset;
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:0.3 animations:^{
[self.view layoutIfNeeded];
}];
}
I have noticed that if I type some text into the first UITextField
, and then tap on the second UITextField
, the text in the first UITextField
jumps upwards and then back down again. If I disable the animation in the above code fragment, this behaviour goes away. So my guess is that when editing a UITextField
, some other constraints are set, which are then altered as focus moves away from that UITextField
. As I'm telling the view to update it's constraints in an animated fashion, this causes the text to move around.
Is there some way I can avoid this, but still maintain the animation for moving the contentView up and down?
Edit: Adding an extra [self.view layoutIfNeeded]
call before I update any constraints fixed the issue. I'm still not to sure what might have been going on though, or really why this fixed it. Anyone have any insight?