Adding UITextField on UIView Programmatically Swift

前端 未结 9 1258
不知归路
不知归路 2021-01-30 06:42

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

相关标签:
9条回答
  • 2021-01-30 07:31

    Make sure you're in the viewDidLoad() and then simply add the following code to produce a textField:

    let myTextField: UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200.00, height: 40.00));
        self.view.addSubview(myTextField)
    
        myTextField.backgroundColor = UIColor.redColor()
        myTextField.text = "some string"
        myTextField.borderStyle = UITextBorderStyle.Line
    
    0 讨论(0)
  • 2021-01-30 07:41

    Swift 3 & Swift 4.2:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let sampleTextField =  UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
        sampleTextField.placeholder = "Enter text here"
        sampleTextField.font = UIFont.systemFont(ofSize: 15)
        sampleTextField.borderStyle = UITextField.BorderStyle.roundedRect
        sampleTextField.autocorrectionType = UITextAutocorrectionType.no
        sampleTextField.keyboardType = UIKeyboardType.default
        sampleTextField.returnKeyType = UIReturnKeyType.done
        sampleTextField.clearButtonMode = UITextField.ViewMode.whileEditing
        sampleTextField.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
        sampleTextField.delegate = self
        self.view.addSubview(sampleTextField)
    }
    

    Delegate Methods:

    // MARK:- ---> UITextFieldDelegate
    
    extension ViewController: UITextFieldDelegate {
    
        func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
            // return NO to disallow editing.
            print("TextField should begin editing method called")
            return true
        }
    
        func textFieldDidBeginEditing(_ textField: UITextField) {
            // became first responder
            print("TextField did begin editing method called")
        }
    
        func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
            // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
            print("TextField should snd editing method called")
            return true
        }
    
        func textFieldDidEndEditing(_ textField: UITextField) {
            // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
            print("TextField did end editing method called")
        }
    
        func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
            // if implemented, called in place of textFieldDidEndEditing:
            print("TextField did end editing with reason method called")
        }
    
        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            // return NO to not change text
            print("While entering the characters this method gets called")
            return true
        }
    
        func textFieldShouldClear(_ textField: UITextField) -> Bool {
            // called when clear button pressed. return NO to ignore (no notifications)
            print("TextField should clear method called")
            return true
        }
    
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            // called when 'return' key pressed. return NO to ignore.
            print("TextField should return method called")
            // may be useful: textField.resignFirstResponder()
            return true
        }
    
    }
    
    // MARK: UITextFieldDelegate <---
    

    Swift 2.0:

    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)
    

    Delegate Methods:

        // MARK:- ---> Textfield Delegates
        func textFieldDidBeginEditing(textField: UITextField) {
            print("TextField did begin editing method called")
        }
    
        func textFieldDidEndEditing(textField: UITextField) {
            print("TextField did end editing method called")
        }
    
        func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
            print("TextField should begin editing method called")
            return true;
        }
    
        func textFieldShouldClear(textField: UITextField) -> Bool {
            print("TextField should clear method called")
            return true;
        }
    
        func textFieldShouldEndEditing(textField: UITextField) -> Bool {
            print("TextField should snd editing method called")
            return true;
        }
    
        func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
            print("While entering the characters this method gets called")
            return true;
        }
    
        func textFieldShouldReturn(textField: UITextField) -> Bool {
            print("TextField should return method called")
            textField.resignFirstResponder();
            return true;
        }
        // MARK: Textfield Delegates <---
    
    0 讨论(0)
  • 2021-01-30 07:41

    You need to add text field to self.view instead of self.

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

    For add translucent blur effect there are already a couple of suggestions available, you can go through -

    UIView border with fade or blur effect

    0 讨论(0)
提交回复
热议问题