Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get auto layout to reposition a view when it's sibling view changes size?
    text
    copied!<p>I have a view controller with two views (greenView, redView) and a switch:</p> <p><img src="https://i.stack.imgur.com/Tkhhb.png" alt="enter image description here"></p> <p>I have a constraint on the redView top space of 50px from greenView. Here are all the constraints on redView:</p> <p><img src="https://i.stack.imgur.com/KNxlS.png" alt="enter image description here"></p> <p>I have a switch that when tapped toggles the height of greenView from 50px to 100px.</p> <p>When I launch the app, the views are laid out as I want, but when the switch is tapped, the greenView frame changes from a height of 50 to 100, the redView doesn't do what I would expect - which is to shift it's y position 50px down to maintain the top space constraint it was assigned. I have an inequality constraint put on the bottom space of redView so there are no conflicts, and I am also calling <code>[self.redView layoutIfNeeded]</code>.</p> <p>Here's the relevant code:</p> <pre><code>- (IBAction)switchTapped { if (theSwitch.isOn) { greenView.frame = CGRectMake(0, 30, 320, 100); [redView layoutIfNeeded]; } else { greenView.frame = CGRectMake(0, 30, 320, 50); [redView layoutIfNeeded]; } </code></pre> <p>Why isn't redView's y position being updated?</p> <p>I have watched WWDC videos, but they seem to mention calling layoutIfNeeded in passing. What am I missing?</p> <p>Here's what it looks like when launched:</p> <p><img src="https://i.stack.imgur.com/Shmr6.png" alt="enter image description here"></p> <p>And here it is when the switch is tapped:</p> <p><img src="https://i.stack.imgur.com/FDgdW.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload