Jack Daniel
Jack Daniel

Reputation: 2467

Add UITextField with constraints programmatically

In my application I want to add one or more UITextFields with button clicking. I can create UITextFields inside a custom UIView (predefined in Storyboard), but I can't properly Add constraints to the text field and constraint for UIView against Button (button is locating under the UIView). So with every button click I want to resize the UIView with given constraints and move button down after adding the text field. now I'm using this code, it adds text fields successfully, but constraints are not working:

@IBAction func addCustomTextField(sender: AnyObject) {

    let x : CGFloat = 20
    var y : CGFloat = 10
    let width : CGFloat = 300
    let height : CGFloat = 40


    var lastButtonY : Int? = NSUserDefaults.standardUserDefaults().integerForKey("lastButtonY")

    if lastButtonY > 0 {
        y = CGFloat.init(lastButtonY! + 8)
        NSUserDefaults.standardUserDefaults().setObject(y + height, forKey: "lastButtonY")
    } else {
        NSUserDefaults.standardUserDefaults().setObject(10 + height, forKey: "lastButtonY")
    }

    sampleTextField = UITextField(frame: CGRectMake(x, y, width, height) )
    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
    self.customView.addSubview(sampleTextField)

    let bottomConstraintForTextField = NSLayoutConstraint(item: sampleTextField, attribute: .Bottom, relatedBy: .Equal, toItem: customView, attribute: .Bottom, multiplier: 1, constant: 10)

    let bottomConstraintForCustomView = NSLayoutConstraint(item: customView, attribute: .Bottom, relatedBy: .Equal, toItem: addButton, attribute: .Bottom, multiplier: 1, constant: 10)

    NSLayoutConstraint.activateConstraints([bottomConstraintForTextField, bottomConstraintForCustomView])

}

Upvotes: 0

Views: 2244

Answers (1)

Varun
Varun

Reputation: 759

As UlyssesR mentioned, you can make use of tableView and handle the insertion easily.

But if you want it to work in this manner, then first of all, you are not specifying enough constraints to hold the view in position.

Basically, assigning a bottom constraint will only identify the y position for the view. So there is no indication for the x position, width or height etc.

In order it to work you have to provide with enough constraints. Possibly around 4 for each view. That really depends on the constraints and requirements.

So in your case, you can add width, height and centerX constraint to hold the view.

Whenever you try to set constraints programmatically, do not forget to set translatesAutoresizingMaskIntoConstraints to false for each view to which the constraints are being applied. Do not set to the parent view if the constraints do not affect the parent view.

Also if you are new to AutoLayouts, I would really suggest you to go through Apple's documentation for autolayouts

There is tutorial for autolayouts in RayWenderLich.

Upvotes: 1

Related Questions