Home:ALL Converter>Adding UITextField on UIView Programmatically Swift

Adding UITextField on UIView Programmatically Swift

Ask Time:2014-07-12T13:45:58         Author:user3763693

Json Formatter

I'm new to Swift and iOS programming. I was trying to create a UITextField programmatically using Swift but couldn't get it quite right. Also how do you change it to have a translucent effect to fade into the background?

var myField: UITextField = UITextField (frame:CGRectMake(10, 10, 30, 10));
self.addSubview(myField)

Author:user3763693,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/24710041/adding-uitextfield-on-uiview-programmatically-swift
Ashok :

Swift 3 & Swift 4.2:\n\noverride func viewDidLoad() {\n super.viewDidLoad()\n\n let sampleTextField = UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))\n sampleTextField.placeholder = \"Enter text here\"\n sampleTextField.font = UIFont.systemFont(ofSize: 15)\n sampleTextField.borderStyle = UITextField.BorderStyle.roundedRect\n sampleTextField.autocorrectionType = UITextAutocorrectionType.no\n sampleTextField.keyboardType = UIKeyboardType.default\n sampleTextField.returnKeyType = UIReturnKeyType.done\n sampleTextField.clearButtonMode = UITextField.ViewMode.whileEditing\n sampleTextField.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center\n sampleTextField.delegate = self\n self.view.addSubview(sampleTextField)\n}\n\n\nDelegate Methods:\n\n// MARK:- ---> UITextFieldDelegate\n\nextension ViewController: UITextFieldDelegate {\n\n func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {\n // return NO to disallow editing.\n print(\"TextField should begin editing method called\")\n return true\n }\n\n func textFieldDidBeginEditing(_ textField: UITextField) {\n // became first responder\n print(\"TextField did begin editing method called\")\n }\n\n func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {\n // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end\n print(\"TextField should snd editing method called\")\n return true\n }\n\n func textFieldDidEndEditing(_ textField: UITextField) {\n // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called\n print(\"TextField did end editing method called\")\n }\n\n func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {\n // if implemented, called in place of textFieldDidEndEditing:\n print(\"TextField did end editing with reason method called\")\n }\n\n func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {\n // return NO to not change text\n print(\"While entering the characters this method gets called\")\n return true\n }\n\n func textFieldShouldClear(_ textField: UITextField) -> Bool {\n // called when clear button pressed. return NO to ignore (no notifications)\n print(\"TextField should clear method called\")\n return true\n }\n\n func textFieldShouldReturn(_ textField: UITextField) -> Bool {\n // called when 'return' key pressed. return NO to ignore.\n print(\"TextField should return method called\")\n // may be useful: textField.resignFirstResponder()\n return true\n }\n\n}\n\n// MARK: UITextFieldDelegate <---\n\n\n\n\nSwift 2.0:\n\nlet sampleTextField = UITextField(frame: CGRectMake(20, 100, 300, 40))\nsampleTextField.placeholder = \"Enter text here\"\nsampleTextField.font = UIFont.systemFontOfSize(15)\nsampleTextField.borderStyle = UITextBorderStyle.RoundedRect\nsampleTextField.autocorrectionType = UITextAutocorrectionType.No\nsampleTextField.keyboardType = UIKeyboardType.Default\nsampleTextField.returnKeyType = UIReturnKeyType.Done\nsampleTextField.clearButtonMode = UITextFieldViewMode.WhileEditing;\nsampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.Center\nsampleTextField.delegate = self\nself.view.addSubview(sampleTextField)\n\n\nDelegate Methods:\n\n // MARK:- ---> Textfield Delegates\n func textFieldDidBeginEditing(textField: UITextField) {\n print(\"TextField did begin editing method called\")\n }\n\n func textFieldDidEndEditing(textField: UITextField) {\n print(\"TextField did end editing method called\")\n }\n\n func textFieldShouldBeginEditing(textField: UITextField) -> Bool {\n print(\"TextField should begin editing method called\")\n return true;\n }\n\n func textFieldShouldClear(textField: UITextField) -> Bool {\n print(\"TextField should clear method called\")\n return true;\n }\n\n func textFieldShouldEndEditing(textField: UITextField) -> Bool {\n print(\"TextField should snd editing method called\")\n return true;\n }\n\n func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {\n print(\"While entering the characters this method gets called\")\n return true;\n }\n\n func textFieldShouldReturn(textField: UITextField) -> Bool {\n print(\"TextField should return method called\")\n textField.resignFirstResponder();\n return true;\n }\n // MARK: Textfield Delegates <---\n",
2015-09-16T07:36:11
Kumar KL :

self.addSubview(myTextField)\n\n\nCould work only, if you're working with the subclass of UIView .\n\nYou have to add it as a subview of the UIView of the ViewController's hierarchy.\n\nvar txtField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 500.00, height: 30.00));\nself.view.addSubview(txtField)\n\n\nBy default background colour is white, it just might not appear. You need to provide the style as well as text colour and background colour.\n\ntxtField.borderStyle = UITextBorderStyle.Line\ntxtField.text = \"myString\" \ntxtField.backgroundColor = UIColor.redColor()\n\n\nFor more info, here",
2014-07-12T06:35:24
deijmaster :

Updated for Swift 3 and Xcode 8.2\n\n let stepsTextField = UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))\n stepsTextField.placeholder = \"Enter text here\"\n stepsTextField.font = UIFont.systemFont(ofSize: 15)\n stepsTextField.borderStyle = UITextBorderStyle.roundedRect\n stepsTextField.autocorrectionType = UITextAutocorrectionType.no\n stepsTextField.keyboardType = UIKeyboardType.default\n stepsTextField.returnKeyType = UIReturnKeyType.done\n stepsTextField.clearButtonMode = UITextFieldViewMode.whileEditing;\n stepsTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center\n",
2016-12-15T03:46:06
rishi :

You need to add text field to self.view instead of self.\n\nvar myField: UITextField = UITextField (frame:CGRectMake(10, 10, 30, 10));\nself.view.addSubview(myField) \n\n\nFor add translucent blur effect there are already a couple of suggestions available, you can go through -\n\nUIView border with fade or blur effect",
2014-07-12T06:23:42
Yalamandarao :

Try below code\n\nvar textFiled = UITextField(frame: CGRectMake(20.0, 30.0, 100.0, 33.0))\n textFiled.backgroundColor = UIColor.redColor()\n textFiled.borderStyle = UITextBorderStyle.Line\n self.view.addSubview(textFiled)\n",
2015-01-06T10:38:20
Sour LeangChhean :

You can use below code with Swift 5.2:\n\nlazy var titleTextField:UITextField = {\n let textField = UITextField()\n textField.translatesAutoresizingMaskIntoConstraints = false\n textField.placeholder = \"Title *\"\n textField.keyboardType = UIKeyboardType.default\n textField.returnKeyType = UIReturnKeyType.done\n textField.autocorrectionType = UITextAutocorrectionType.no\n textField.font = UIFont.systemFont(ofSize: 13)\n textField.borderStyle = UITextField.BorderStyle.roundedRect\n textField.clearButtonMode = UITextField.ViewMode.whileEditing;\n textField.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center\n return textField\n}()\n",
2020-03-02T14:59:21
Mahendra Vishwakarma :

Add the textField on UIView is\n\nvar textField=UITextField(frame:CGRectMake(x,y,width,height));\nself.view.addSubView(textField)\n",
2015-10-30T06:25:19
Amiru Homushi :

Make sure you're in the viewDidLoad() and then simply add the following code to produce a textField:\n\nlet myTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200.00, height: 40.00));\n self.view.addSubview(myTextField)\n\n myTextField.backgroundColor = UIColor.redColor()\n myTextField.text = \"some string\"\n myTextField.borderStyle = UITextBorderStyle.Line\n",
2016-08-18T20:54:24
yy