我有一个按钮,可以动态地将UITextField添加到xcode中的视图中。我想继续按下按钮,在视图下面添加尽可能多的文本字段,但我不知道该怎么做
到目前为止,这是我的代码:
@IBAction func buttonPressed(sender: AnyObject) {
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)
print(sampleTextField.text!)
}
我知道这会添加一个texfield,按下按钮会重叠新的文本字段 我想按下按钮并添加另一个
答案 0 :(得分:0)
由于您需要下面的下一个文本字段,您需要修改函数中的y帧。
创建一个变量来保存y原点值,并在函数末尾添加它的值。
var yValue = 100
@IBAction func buttonPressed(sender: AnyObject) {
let sampleTextField = UITextField(frame: CGRectMake(20, yValue, 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)
print(sampleTextField.text!)
yValue += 50
}