when how appears ios iphone uitextfield uitextview uikeyboard

ios - how - scroll to textfield swift



UITextField y notificaciones de teclado-orden extraño (2)

Así que he configurado una notificación para el evento de aparición del teclado. Ahora consideremos un UITextView y un UITextField.

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

El selector es:

- (void)keyboardWillShow:(NSNotification *)notification { keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; }

En el caso de un UITextView, el método delegado - (void)textViewDidBeginEditing:(UITextView *)textView se - (void)textViewDidBeginEditing:(UITextView *)textView DESPUÉS del - (void)textViewDidBeginEditing:(UITextView *)textView keyboardWillShow: método. Entonces keyboardSize tiene el tamaño real del teclado y puedo usarlo dentro del método de delegado de vista de texto.

Sin embargo, en el caso de un UITextField, el método de delegado correspondiente - (void)textFieldDidBeginEditing:(UITextField *)textField se - (void)textFieldDidBeginEditing:(UITextField *)textField ANTES del - (void)textFieldDidBeginEditing:(UITextField *)textField keyboardWillShow: método.

¿Por qué esto es tan? ¿Cómo obtengo el CGSize del teclado en el caso del campo de texto ya que ahora solo devuelve cero porque el delegado del campo de texto se llama primero y no el selector del teclado?


Extraño ... Suena como un error en el final de Apple.

Tal vez usted podría retrasar el teclado apareciendo? Aquí está mi desafortunada sugerencia de "trabajo alrededor": puede enviar una notificación cuando se selecciona el campo de texto, pero luego solo comienza a editar una fracción de segundo más tarde para que el campo de texto se conozca antes que keyboardWillShow: se llame . Por ejemplo:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { // Notification corresponding to "textFieldSelected:" method [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_TEXT_FIELD_SELECTED object:nil userInfo:[[NSDictionary alloc] initWithObjectsAndKeys:textField, @"textField", nil]]; // "textFieldReallyShouldBeginEditing" is initially set as FALSE elsewhere in the code before the text field is manually selected if (textFieldReallyShouldBeginEditing) return YES; else return NO: } - (void)textFieldSelected:(NSNotification*)notification { // Done in a separate method so there''s a guaranteed delay and "textFieldReallyShouldBeginEditing" isn''t set to YES before "textFieldShouldBeginEditing:" returns its boolean. [self performSelector:@selector(startTextFieldReallyEditing:) withObject:(UITextField*)notification[@"textField"] afterDelay:.01]; } - (void)startTextFieldReallyEditing:(UITextField*)textField { textFieldReallyShouldBeginEditing = YES; // To trigger the keyboard [textField becomeFirstResponder]; }

Luego, dependiendo de cómo esté creando la notificación, puede insertar el valor de este campo de texto ahora conocido incluso antes de que comience la edición.


He tenido este mismo problema. Trate de usar:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView