39

How to programmatically add UITextField on UIView in iPhone programming?

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

[view addSubview:???];
djot
  • 2,952
  • 4
  • 19
  • 28
suse
  • 10,503
  • 23
  • 79
  • 113

4 Answers4

250

If you want one looking like the text field in Interface builder:

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];
  • 8
    `contentVerticalAlignment` is the crucial element here – Nate Symer Feb 20 '13 at 23:11
  • +1 @NathanielSymer : I spent 1 hour to figure that until I see your comment here... :) Thanks.. – Tek Yin Mar 22 '13 at 11:06
  • 1
    big thank to @ioshero this is really a good answer... IMHO I think should be marked as a correct answered. the fact you covered alot of the different parameters of the UITextField is just great. Once again, thank you for your answer. – Jiraheta Sep 05 '13 at 20:37
34

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)
Skie
  • 1,942
  • 16
  • 27
3

In 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)

In 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)
Ashok
  • 5,585
  • 5
  • 52
  • 80
3
UITextField *mycooltext=[[UITextField alloc]initWithFrame:CGRectMake(10, 70, 50, 50)];
mycooltext.text=@"I am cool";
[self.view addSubview:mycooltext];
Craig Conover
  • 4,710
  • 34
  • 59
sandeep nag
  • 148
  • 8