Changing the multiplier of a constraint based on size class

此生再无相见时 提交于 2019-12-05 19:09:53

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/27227985/changing-the-multiplier-of-a-constraint-based-on-size-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!