Changing auto-layout constraints at runtime

I have a named constraint so that I can change the height of an iOSImageView at runtime, depending on the screen size i.e. the image’s height is reduced for smaller iPhones.

Below the iOSImageView I have an iOSLabel which has a Top constraint equal to the iOSImageView.bottom constraint. I had hoped that resizing the iOSImageView at runtime would automatically move the iOSLabel but alas, it stays where it is.

I could, of course, name the constraint of the iOSLabel.top, so that I could also move it at runtime, but there are actually several rows of iOSLabels, each one with a top constraint equal to the bottom of the label above it so I was trying to do this smartly.

tl;dr I hoped that changing the offset of one constraint at runtime, would update the positioning of any controls tied to it. But it doesn’t.

Any ideas/solutions? Thanks.

What aspect of the constraint are you changing ?

I’m reducing the height offset of the iOSImageView at runtime. iOSLabel.top is set to equal iOSImageView.bottom. So therefore, as the iOSImageView.bottom changes, I was hoping the iOSLabel’s top would change also.

I wonder if its not marking the layout as needing to be solved again
That would be consistent with what you’re seeing

Not sure of the declare off the top of my head but a declare to UIView layoutIfNeeded might work around this in the mean time

Oh and if you can file a bug report with a sample

Okay, finally tracked it down. I was changing the width offset of the iOSImageWell which was visibly resizing the image correctly, as the ContentMode was set to ScaleAspectFit, so all looked good except the label wasn’t moving. When I changed the height offset of the iOSImageWell, then the label moved correctly too.

In other words, the visible change to the ImageWell was exactly the same, as it should be, whether I changed the width or height. But the label was only moving when I changed the height of the ImageWell. Hope that makes sense.

So, I don’t know if you consider this a bug or not. I’m happy to create a bug report with sample if you think it’s worth reporting.

ah you made it narrower but not shorter ?
Then that makes sense why the label would not move up
The contents of the imagewell would appear to change its size (aspect fit does that) but the ACTUAL frame of the image well would not move up so there’s no reason for the label to move up as well
Is that what you’re describing ?
If so then yeah its not a bug :slight_smile:

[quote=292928:@Norman Palardy]
The contents of the imagewell would appear to change its size (aspect fit does that) but the ACTUAL frame of the image well would not move up so there’s no reason for the label to move up as well[/quote]
Thanks, I assumed the whole thing was resizing (the whole control including frame), and you know what assumptions make.

[quote=292928:@Norman Palardy]
If so then yeah its not a bug :)[/quote]
:slight_smile: