En el contexto de la iPhone:UIKit: UIScrollView desplazándose automáticamente cuando un subvista aumenta su anchura más allá del borde de la pantalla
Tengo un UIScrollView que contiene un UIImage. Cuando el usuario toca la pantalla dentro del UIImage, se agrega un UITextField donde tocó el usuario. El usuario puede editar este UITextField, y el campo de texto se redimensionará automáticamente en función de si el texto fue agregado o eliminado.
Cuando el UITextField que se está editando aumenta su ancho, la vista de desplazamiento se desplaza automáticamente para mostrar el ancho incrementado.
El problema viene porque el desplazamiento automático del campo de texto no respeta el valor de y de la pantalla
Por ejemplo, dicen que el usuario añade un campo de texto en la parte inferior de la imagen. Cuando vayan a editar ese campo de texto, se mostrará el teclado, ocultando el campo de texto. Tengo un código en su lugar para desplazar la pantalla y mostrar el campo de texto. El problema aparece cuando el usuario ingresa tanto texto que ese campo de texto se extiende más allá del borde de la pantalla. Cuando esto sucede, la pantalla se desplaza horizontalmente para ajustarse al texto más ancho, pero también verticalmente: el desplazamiento vertical termina ocultando el campo de texto, anulando básicamente todo lo que hice para mostrar el campo de texto.
Código para mostrar el campo de texto si está oculto por el teclado:
- (void)keyboardWasShown:(NSNotification*)notification
{
NSDictionary* info = [notification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
self.offset = self.contentOffset;
CGRect frame = self.frame;
// self.activeField is the name of the field that is the current first responder - this just adds a little bit of padding
frame.size.height -= keyboardSize.height + (self.activeField.frame.size.height * 2);
if (!CGRectContainsPoint(frame, self.activeField.frame.origin)) {
CGPoint scrollPoint = CGPointMake(self.offset.x, self.activeField.frame.origin.y - keyboardSize.height + (activeField.frame.size.height * 2));
[self setContentOffset:scrollPoint animated:YES];
}
}
Este es el código para aumentar el tamaño del campo de texto:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
CGSize stringSize = [string sizeWithFont:textField.font];
CGSize newSize = [newString sizeWithFont:textField.font];
// Make textField wider if we're close to running up against it
if (newSize.width > (textField.frame.size.width - self.widthOffset)) {
CGRect textFieldFrame = textField.frame;
if (stringSize.width > self.widthOffset) {
textFieldFrame.size.width += stringSize.width;
}
textFieldFrame.size.width += self.widthOffset;
textField.frame = textFieldFrame;
}
// Shrink the textField if there is too much wasted space
if ((textField.frame.size.width - newSize.width) > self.widthOffset) {
CGRect textFieldFrame = textField.frame;
textFieldFrame.size.width = newSize.width + self.widthOffset;
textField.frame = textFieldFrame;
}
return YES;
}
La pregunta es: ¿Cómo ¿Obtengo que UIScrollView respeta el valor y de sí mismo cuando se desplaza automáticamente?
Awesome question! – Legolas
Ayudaría si puede proporcionar un enlace con un proyecto de muestra. –