web-dev-qa-db-fra.com

Ajouter UITextField sur UIView par programme

Comment ajouter par programmation UITextField sur UIView dans la programmation iPhone?

UITextField* text;
UIView* view = [[UIView alloc]init];

[view addSubview:???];
36
suse

Objectif 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];

Rapide:

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)
32
Skie

Si vous en voulez un qui ressemble au champ de texte dans le générateur d'interface:

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];
243
user263865

Dans Swift 2.0:

let sampleTextField = UITextField(frame: CGRectMake(20, 100, 300, 40))
sampleTextField.placeholder = "placeholderText"
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)

Dans Swift 3.0:

let sampleTextField = UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
sampleTextField.placeholder = "placeholderText"
sampleTextField.font = UIFont.systemFont(ofSize: 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)
2
Ashok
UITextField *mycooltext=[[UITextField alloc]initWithFrame:CGRectMake(10, 70, 50, 50)];
mycooltext.text=@"I am cool";
[self.View addSubView:mycooltext];
2
sandeep nag