Search code examples
iosobjective-cuitextfielduikeyboarduianimation

Switching between UITextFields


I have a view with additional UIView that in fact is a container for 2 UITextFields

When any of these text fiels become first responder i need to move them up because when keyboard is opened text fields are not visible. I am handling UIKeyboardDidShowNotification and UIKeyboardDidHideNotification and change frame of container view with text fields like this:

#pragma mark - Keyboard notifications handling

- (void) keyboardIsShown:(NSNotification *)notification {
    // Moving up text field while keyboard is opened

    CGRect containerFrame = self.viewContainerCredentials.frame;
    containerFrame.origin.y -= kCredentialsViewOffset;

    [UIView beginAnimations:@"moveUp" context:nil];
    [UIView setAnimationDuration:0.5f];
    self.viewContainerCredentials.frame = containerFrame;
    [UIView commitAnimations];
}

- (void) keyboardIsHidden:(NSNotification *)notification {
    // Moving down text field while keyboard is closed

    CGRect containerFrame = self.viewContainerCredentials.frame;
    containerFrame.origin.y += kCredentialsViewOffset;

    [UIView beginAnimations:@"moveDown" context:nil];
    [UIView setAnimationDuration:0.5f];
    self.viewContainerCredentials.frame = containerFrame;
    [UIView commitAnimations];
}

When I activate one of text fields - everything is working perfect and when I close the keyboard - view container moves back down correctly as well.

But when I tap first field and view container is moved up and then I tap and activate second text field without closing the keyboard - my view container restores its initial frame and goes back under the keyboard.

Can anybody help with this? Why does it happen?

Thank you in advance.

UPD: Problem was deeper: there wrongly configured auto layouts for view container that were pushed back after each resigning of first responder.


Solution

  • Problem was deeper - there were wrongly configure autolayouts for view container that were pushed back by system after making first responder any other control on main view.