Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textFieldDidEndEditing moves the screen when not wanted

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?

like image 313
user700352 Avatar asked Jul 02 '12 20:07

user700352


1 Answers

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);
}
like image 75
Sarreph Avatar answered Oct 05 '22 23:10

Sarreph