以编程方式在UIView上添加UITextField

39

在 iPhone 编程中如何以编程方式在 UIView 上添加 UITextField

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

[view addSubview:???];
4个回答

250
如果您想要一个类似于界面生成器中的文本字段:
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是这里的关键元素。 - Nate Symer
+1 @NathanielSymer:我花了一个小时才明白,直到看到你在这里的评论... :) 谢谢.. - Tek Yin
1
非常感谢@ioshero,这真的是一个很好的答案...依我之见,我认为它应该被标记为正确答案。你涵盖了许多不同的UITextField参数,这真的很棒。再次感谢你的回答。 - Jiraheta

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)

什么是帧? - galactikuh
1
@galactikuh 你是什么意思?Frames 是一个矩形,在其中会放置你的视图。 - Skie
如果这是答案的重要部分,那么创建框架的代码应该包含在内。 - galactikuh
1
不多,但我在 Obj-C 部分添加了创建。 - Skie

3
UITextField *mycooltext=[[UITextField alloc]initWithFrame:CGRectMake(10, 70, 50, 50)];
mycooltext.text=@"I am cool";
[self.view addSubview:mycooltext];

3

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接