2

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??

learn phase
  • 103
  • 10

5 Answers5

2

Constraints ignore the hidden property.

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

See this example.

Community
  • 1
  • 1
shallowThought
  • 19,212
  • 9
  • 65
  • 112
1

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

self.view.layoutIfNeeded()

Tj3n
  • 9,837
  • 2
  • 24
  • 35
  • where should i add this??please elaborate..And does this support Autolayout? – learn phase Nov 17 '16 at 10:58
  • just after the code above, after you change your constraint you have to call this, and yes, this tell your auto layout to layout again with new constraint – Tj3n Nov 17 '16 at 10:59
  • It should be working, if not then your constraint is wrong or it just doesnt change anything – Tj3n Nov 17 '16 at 11:15
  • I have added it after self.middleViewHeightConstraint.constant = 0. it does not changes any thing. – learn phase Nov 17 '16 at 11:17
  • It may matter what type of subviews are in the view. I learned the hard way last night that UISlider, even when hidden, has an intrinsic width no matter what. My only solution (so far) is to "live with it". –  Nov 17 '16 at 12:46
1
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.

Arnab
  • 4,216
  • 2
  • 28
  • 50
1

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

PGDev
  • 23,751
  • 6
  • 34
  • 88
-1

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

Aakash
  • 2,239
  • 15
  • 23
  • If you are using AutoLayout, changing a view's frame has no impact. –  Nov 17 '16 at 14:40
  • It worked for me that is why I suggested to try this because it happened to me sometimes that when I try to change the constraint does nothing but changing the frame does. – Aakash Nov 18 '16 at 13:23