2010-08-09 6 views
5

Tengo un UITextField en una UITableViewCell. Por alguna razón, el botón borrar no se alinea con el texto del campo textField. Aquí está mi código textField¿Por qué el botón claro no está alineado con el texto en un UITextField?


cell.selectionStyle = UITableViewCellSelectionStyleNone; 
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(22, 10, cell.contentView.frame.size.width, cell.contentView.frame.size.height)]; 
usernameField.adjustsFontSizeToFitWidth = YES; 
usernameField.placeholder = @"Username"; 
usernameField.backgroundColor = [UIColor clearColor]; 
usernameField.keyboardType = UIKeyboardTypeDefault; 
usernameField.autocapitalizationType = UITextAutocorrectionTypeNo; 
usernameField.autocorrectionType = UITextAutocorrectionTypeNo; 
usernameField.returnKeyType = UIReturnKeyNext; 
usernameField.delegate = self; 
usernameField.clearButtonMode = UITextFieldViewModeAlways; 
[usernameField setEnabled:YES]; 
[cell addSubview:usernameField]; 
[usernameField becomeFirstResponder]; 
[usernameField release]; 

y aquí está la imagen para demostrar el problema: Screenshot

Editar: También probé con - (CGRect)clearButtonRectForBounds:(CGRect)bounds y todavía nada

Respuesta

19

¿Cuál es su propiedad contentVerticalAlignment? He visto este problema aparecer cuando el botón Borrar está alineado al centro pero el texto está alineado en la parte superior. Intente configurar la alineación de su campo de texto a lo siguiente:

usernameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 

¿Alguna suerte con eso?

+0

Muchas gracias por ayudarnos, pero ahora el texto y el botón borrar están alineados en la parte inferior de la celda. Este código realmente alinea el texto, no el botón borrar. Quiero alinear el botón borrar con el centro de la celda ¿Alguna sugerencia? –

+0

¡Funcionó! Acabo de cambiar los atributos de CGRect para que quepan en el botón de borrar –

+0

Gracias. para aclarar, tomé en cuenta la altura de la celda y el campo de texto, para corregir el desplazamiento vertical. CGRect newFrame = albumTitleTextField.frame; newFrame.origin.y = (cell.bounds.size.height - newFrame.size.height)/2.0; [albumTitleTextField setFrame: newFrame]; –

Cuestiones relacionadas