Cómo agregar programáticamente UITextField
en UIView
en la programación de iPhone?Agregar UITextField en UIView programmatically
UITextField* text;
UIView* view = [[UIView alloc]init];
[view addSubview:???];
Cómo agregar programáticamente UITextField
en UIView
en la programación de iPhone?Agregar UITextField en UIView programmatically
UITextField* text;
UIView* view = [[UIView alloc]init];
[view addSubview:???];
Objective-C:
CGRect someRect = CGRectMake(0.0, 0.0, 100.0, 30.0);
UITextField* text = [[UITextField alloc] initWithFrame:someRect];
UIView* view = [[UIView alloc] initWithFrame:someRect];
[view addSubview:text];
Swift:
let someFrame = CGRect(x: 0.0, y: 0.0, width: 100.0, height: 30.0)
let text = UITextField(frame: someFrame)
let view = UIView(frame: someFrame)
view.addSubview(text)
Si quieres uno mirando como el campo de texto en el constructor de interfaz:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
[textField release];
'contentVerticalAlignment' es el elemento crucial aquí –
+1 @NathanielSymer: Pasé 1 hora para entender eso hasta que veo tu comentario aquí ... :) Gracias .. –
gracias a @ioshero esta es realmente una buena respuesta ... en mi humilde opinión, creo que debería marcarse como una respuesta correcta. El hecho de que haya cubierto muchos de los diferentes parámetros del UITextField es simplemente genial. Una vez más, gracias por tu respuesta. – Jiraheta
En Swift 2,0:
let sampleTextField = UITextField(frame: CGRectMake(20, 100, 300, 40))
sampleTextField.placeholder = "Enter text here"
sampleTextField.font = UIFont.systemFontOfSize(15)
sampleTextField.borderStyle = UITextBorderStyle.RoundedRect
sampleTextField.autocorrectionType = UITextAutocorrectionType.No
sampleTextField.keyboardType = UIKeyboardType.Default
sampleTextField.returnKeyType = UIReturnKeyType.Done
sampleTextField.clearButtonMode = UITextFieldViewMode.WhileEditing;
sampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
sampleTextField.delegate = self
self.view.addSubview(sampleTextField)
UITextField *mycooltext=[[UITextField alloc]initWithFrame:CGRectMake(10, 70, 50, 50)];
[email protected]"I am cool";
[self.View addSubView:mycooltext];
¿Cuáles son los marcos? – galactikuh
@galactikuh ¿qué quieres decir? Los marcos son rectángulos en los que se colocarán sus puntos de vista. – Skie
si esta es una parte importante de la respuesta, parece que la creación del marco debe incluirse en su código. – galactikuh