I have a form with many Label and text Field, I want to show some Text Field only when a Picker View was selected, so I use hidden = true
to hide my items, but there's a gap between two text fields because the middle the text field is hidden. How do I do it so I don't have a blank space ?
Screen:
You can accomplish what you want by using Auto Layout constraints with differing priorities.
8
and a priority of 998
.8
and a priority of 997
.@IBOutlet
to the constraint in step 2 to your code. You can do this by finding the constraint in the Document Outline and control-dragging to your code. Call it something like field3Top
.When it is time to hide field 2, set the hidden
property for your text field 2 and label 2 to true
and change the priority of fieldTop.priority
to 999
. This will cause this constraint to have priority over the other one and the gap will be closed.
@IBOutlet weak var field3Top: NSLayoutConstraint!
@IBOutlet weak var field2Label: UILabel!
@IBOutlet weak var field2TextField: UITextField!
field3Top.priority = 999
field2Label.hidden = true
field2TextField.hidden = true
// for fun, you can animate the change with this code
UIView.animateWithDuration(1.0) {
self.view.layoutIfNeeded()
}
When it is time to show field 2 again, set field3Top.priority = 997
and set the hidden
properties back to false
.
Here's a demo of the hiding in action: