6
votes

In my app, my UITextFields are placed below where the top of the keyboard would be, so I have to move my view up so that the text field is visible over the keyboard when I'm editing. In order to do this I use the UITextFieldDelegate method, textFieldDidBeginEditing:. Then in the method textFieldDidEndEditing:, I move the view back down.

The problem occurs when I try to switch between text fields. When I do, the delegate calls the method textFieldDidEndEditing: then immediately calls textFieldDidBeginEditing: for the other text field, which makes the view go down then immediately up, so it looks like the screen jolts. Is there any workaround for this effect?

4

4 Answers

2
votes

I've just been having the exact same issue, and this is the solution that I came to.

Set up a separate method for handling when your textField's keyboard is resigned, and place your self.view.center realignment in there. This way, you can ensure that your textFieldDidEndEditing method is kept separate.

Here's an example if I haven't explained myself properly. Note that when the user taps on a textField, I place a DONE button in the navigation bar (due to it being a numerical keypad), although you can link this method to the DONE button on a normal keyboard:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    UIBarButtonItem *hideKeyboardButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(resignResponder:)];
    [self.navigationItem setRightBarButtonItem:hideKeyboardButton animated:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {

//Nothing needs to go here anymore

}

- (void)resignResponder:(id)sender {
    [textField resignFirstResponder];
    //Use core animation instead of the simple action below in order to reduce 'snapback'
    self.view.center = CGRectMake(0, 0, 320, 480);
}
0
votes

Just check in which text field you are (for example, give them tags). Then according to this information, move or don't move your view.

0
votes

I think the best way to move the textfield is to register for the notifications

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

Do the modification of textfield frame in keyboardWillShow: & keyboardWillHide:

0
votes

I used a different approach to solve this problem: When switching from one edit control to another (field1 -> field2) you receive the following messages:

  • textFieldShouldBeginEditing (field2)
  • textFieldShouldEndEditing (field1)
  • textFieldDidEndEditing (field1)
  • textFieldDidBeginEditing (field2)

What I do is to use a counter variable (x):

  • Add x+=1 in textFieldShouldBeginEditing
  • Add x-=1 in textFieldShouldEndEditing

Then:

  • in textFieldDidEndEditing stretch the controls to original position if x<0
  • in textFieldDidBeginEditing roll up the controls if x>0