Changing constraints to expand a view in swift iOS

614 Views Asked by At

I have a stack with 2 view inside it, the yellow takes up 70% of the stack and the green the other 30%. I want to code a button that will upon click:

  1. Expand the stack to the top of the screen, which I have done via:

myStack.topAnchor.constraint(equalTo: view.topAnchor).isActive = true

  1. Then make the green box take up the whole stack, so the screen will be completely green

I have done the 70:30 thing with "equal widths constraint like so: widths

How may I achieve this functionality and what is the code of resetting everything back to normal? desired output

1

There are 1 best solutions below

2
On BEST ANSWER
  1. Change the stackView distribution to be Fill Proportionally.
  2. Set up width constraints indepedently for each view (with priority = 999, allowed to break). (0.7 of superview.width for first & 0.3 of superview.width for second).
  3. Call firstView.isHidden = true, it will automatically stretch the secondView to cover stackView in full - Expanded state.
  4. Call firstView.isHidden = false, it will bring you back to Initial state.