Changing the multiplier of a constraint based on size class
Asked Answered
P

2

12

Is it possible to give a constraint a different multiplier depending on the current size class?

I have a view that I want to be half the size of the screen for a regular size class width and I want it to be 80% of the size of the screen for a compact size class width. In the storyboard I have the option to add different variables for different size classes to the constraints constant value, but not it's multiplier value. It's an Equal Widths Constraint in case that is relevant.

I haven't done much with adding constraints programatically so I'm hoping their might be a solution that lies down that road. Can anyone tell me if it is possible to do what I'm looking for via storyboard or programatically?

Putt answered 1/12, 2014 at 12:2 Comment(0)
P
24

The solution was painfully obvious, no idea how I missed it. You have to create two different constraints and enable/disable them accordingly.

I think this is a very inelegant solution tho, I would really like if apple would make it so that you can define different multipliers per size class just like the constant variable. Having lots of constraints that only differ by one value seems like it could result in a very unwieldy and hard to maintain storyboard.

If anybody knows of any better solutions, or knows why it is done this way I would still like to hear it.

Putt answered 1/12, 2014 at 12:31 Comment(4)
Definitely agree with you! Just as Apple has given one the ability to define different constants determined by size class on the same constraint, they should do the same with multipliers for that same constraint.Capillary
Did you enable/disable in code or via storyboard settings?Testate
@JohnD. You can enable/disable constraints via the storyboard by selecting the constraint, scrolling to the bottom on the inspector and ticking/unticking the box. I'm sure you can also do it by editing the code directly if you need to.Putt
Just a little extra clarification for anyone who needs it: scroll down in the inspector to "Installed", and click the + button to add the variation needed. Mark the checkbox only for the appropriate class size, and then do the same with the other constraint.Tegan
C
0

I was able to provide different multipliers for each size class by selecting the element (whose constraint needs to be changed)-> size inspector -> constraints select "All" instead of "This Size Class" -> This will show the constraints of both the current size class with the constraints of the unselected size class greyed out. Right-click on the desired constraint (common constraints show up in both size classes) you can change the multiplier values for both the size class constraints.

Cannabis answered 8/8, 2017 at 19:38 Comment(1)
I'm not seeing the behavior you describe. If I have a single constraint with different size class variants added and I follow your instructions and click on "edit" or double-click on it (right-click doesn't do anything), it takes me back to the edit view for that constraint where no possibility exists to change the multiplier based on the size class. I'm using Xcode 9.2.Capillary

© 2022 - 2024 — McMap. All rights reserved.