learn phase
learn phase

Reputation: 113

Swift constraint doesnot change in Autolayout while hiding views

These are my codes //My UIViews

@IBOutlet weak var UIVIewFirst: UIView!
@IBOutlet weak var UIViewSecond: UIView!
@IBOutlet weak var UIViewThird: UIView!

@IBOutlet weak var middleViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var ViewThirdHeight: NSLayoutConstraint!

There is a button to show and hide the view as;

    @IBAction func infoClicked(sender: SSRadioButton) {
    if UIViewSecond.hidden {
        sender.selected = false
        UIViewSecond.hidden = false
        self.middleViewHeightConstraint.constant = 134

    } else {
        sender.selected = true
        UIViewSecond.hidden = true
        self.middleViewHeightConstraint.constant = 0
        self.ViewThirdHeight.constant = 180
    }
}

the vertical gap between each view is 10. After hiding the view the gap becomes 20. But i need it to set it 10 between third and second view. Even though i set the third view height constant to any number it does not changes it position.Can anyone suggest why is this happening??

Upvotes: 2

Views: 1379

Answers (5)

PGDev
PGDev

Reputation: 24341

You need to take the outlet connection of the vertical spacing constraint between either First-Second or Second-Third views. Also if you want to hide/show only Second view, you don't need to do any changes to Third View height constraint.

Say For example, we take the outlet of vertical spacing between First and Second views, then:

@IBOutlet weak var UIVIewFirst: UIView!
@IBOutlet weak var UIViewSecond: UIView!

@IBOutlet weak var middleViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var verticalSpacingConstraint: NSLayoutConstraint!

@IBAction func infoClicked(sender: UIButton)
{
    if UIViewSecond.hidden
    {
        sender.selected = false
        UIViewSecond.hidden = false
        self.middleViewHeightConstraint.constant = 134
        self.verticalSpacingConstraint.constant = 10
    }
    else
    {
        sender.selected = true
        UIViewSecond.hidden = true
        self.middleViewHeightConstraint.constant = 0
        self.verticalSpacingConstraint.constant = 0
    }
}

Below are the screenshots of output:

1. When button is not selected

enter image description here

2. When button is selected

enter image description here

Upvotes: 1

Aakash
Aakash

Reputation: 2269

Try to change the frame of the view instead of changing the constraint, also do view.layoutIfNeeded after making any changes.

Upvotes: -1

shallowThought
shallowThought

Reputation: 19602

Constraints ignore the hidden property.

If possible for your requirements, embedd your views within a UIStackView.

See this example.

Upvotes: 2

Arnab
Arnab

Reputation: 4356

firstView
   | gap = 10
secondView
   | gap = 10
thirdView

after removing secondView

    firstView
       | gap = 10
  ---------- height = 0
       | gap = 10
   thirdView

so the gap becomes 20

try to add constraint programmatically after hiding the view or decrease any one gap.

Upvotes: 1

Tj3n
Tj3n

Reputation: 9923

You have not tell your view to update the view with the new constraint, you have to call this code:

self.view.layoutIfNeeded()

Upvotes: 1

Related Questions