Chris G.
Chris G.

Reputation: 25974

Replace a view in a Autolayout NSConstraints setup

I have used only NSConstraints in Interface Builder to setup a view. Now I have a StackView that I want to animate out. This causes all the Layout to be misplaced.

  1. I have tried to replace the view(StackView) with a temp(with the same frame), but that did not work.
  2. I have also tried to set translatesAutoresizingMaskIntoConstraints

  3. also tried the following:

    self.numbers_Top_Constraint.constant = self.topscreen.frame.size.height
    self.topscreen.setNeedsUpdateConstraints()
    UIView.animate(withDuration: 0.5, animations: {
    
        self.topscreen.layoutIfNeeded()
    

    }

Do you know how to do this?

Upvotes: 0

Views: 45

Answers (1)

Jasveer Singh
Jasveer Singh

Reputation: 374

Use constraints instead of changing frames. Set a top margin constraint to stack view and then change it before the animation and call self.view.layoutIfNeeded(). Something like this -

self.numbersStackViewTopMarginConstriant.constant = self.view.frame.size.height
UIView.animate(withDuration: 0.5, animations: {

            self.view.layoutIfNeeded()

        }) { (Bool) in

Upvotes: 1

Related Questions