1

I would like to animate between two different UIView's that occupy the same space on the screen. Sometimes the user would like to see an Image and other times statistics. By tapping a button I would like to animate between the two.

Right now I only have one of the views arranged inside the stackView in a XIB and I'm wondering how do i set this up without messing up autoLayout?

Do I need to get a reference to the stackView and one view for the other there? And if so does that mean I have to setup layout constraints programatically?

What's the best way to handle this?

Thank you.

aaronium112
  • 2,992
  • 4
  • 35
  • 50

2 Answers2

0

Put both views in a UIStackView. Set outlets to the two subviews. (You don't necessarily need an outlet to the stack view itself.) When you set one of them to isHidden = true, the stack view takes care of "removing" it from the visible content without preserving the frame that it would occupy when non-hidden. In your animation, just animate the isHidden = [true|false] statement.

NRitH
  • 13,441
  • 4
  • 41
  • 44
0

XCode 9.1 and swift 4

I would do this way:

  • If i have two View inside one StackView: set outlets for the two View.
  • In any event like press button, just hidden the View one i want:

self.ViewTwo.isHidden = true

  • And animating like this:

UIView.animate(withDuration: 0.5) { self.ViewTwo.isHidden = true }

  • And when i just want back :

UIView.animate(withDuration: 0.5) { self.ViewTwo.isHidden = false }

The direction of the animation will be according to the settings configured in the stack View (Vertical or Horizontally)

xhinoda
  • 1,032
  • 1
  • 19
  • 26