Programme Constraint Variation

Inside storyboard files, setting constraints and some variation for different device variation, that is with either compact or regular width and height is quite easy.

Now in the code part, setting constraints is also simple but is it possible to set a constraint value for a certain device variation?


For the iPad it would be wR hR and for the iPhone 8 Plus wC hR, so I would like to know whether it is posiible to set a variation in the constraint programmatically, or atleast change the constraint variation value for a certain device programmatically through a constraint outlet.


Thank You and have a good day.


Sincerely yours, SamS19

Answered by junkpile in 313308022

It appears there is no way to access specific variation constant values currently. In code it looks like you only see the "current" value of the constraint constant. And if you modify that constraint constant, UIKit will overwrite it with whatever you specified on the storyboard the next time it does a layout pass anyway. So you would have to adjust the constant *after* UIKit has done its thing (in viewDidLayoutSubviews for example).


It might be easier to not fight the system and either go with all IB or all code (create different sets of constraints and install/uninstall them in viewWillTransitionToSize when the trait collection changes) solutions for now. It would certainly be nice to be able to access the constant values for different trait collections in code... feel free to file an enhancement request 🙂

What do you mean by Constraint Variation or device variation ?


If you mean size class, you can easily detect size classes through `traitCollection` and set up any constraints progorammatically according to the size classes as you like.


If you mean something else, you may need to explain what you really want to do using more common terms in iOS.

This isn’t a question about a feature of the Swift programming language, so it’s posted in the wrong forum. Cocoa Touch might be better.


I don’t think there’s a way to specify to which size class(es) a constraint should apply when you create one in code, like you can in IB. Instead you would have to implement the traitCollectionDidChange: method in your view controller and activate/deactivate/adjust constraints as appropriate. Here’s the first example I found. It’s really old so there may be better ways to do some of it (layout anchors, activating/deactivating, of course using the safe area, etc.) but it gives the general idea.


makeapppie.com/2015/11/25/using-trait-collections-for-auto-layout-and-size-classes/

In my View Controller in the Storyboard file, after setting a new Constraint for an object, select the Constraint and under Attributes inspector, options like Constant, Priority and Multiplier are shown. On the left of the Constant option, click on the small plus button and add a variation based on width and height.

Lets take an example of a variation based on wR hC and set the value as 50.


After that's done connect the Constraint to the class file as an IBOutlet.

Now what I would like to know is it possible to change the value of the Variation added to the Constraint programmatically?

For example instead of changing the Constant value of the Constraint Outlet, can I change the value of the wR hC variation of the constraint programmatically, which is 50 to be changed to 120.

If yes, please explain how.


Sorry for the inconvenience of the first explanation of my question.

And also I'm kind of new to iOS programming.

Accepted Answer

It appears there is no way to access specific variation constant values currently. In code it looks like you only see the "current" value of the constraint constant. And if you modify that constraint constant, UIKit will overwrite it with whatever you specified on the storyboard the next time it does a layout pass anyway. So you would have to adjust the constant *after* UIKit has done its thing (in viewDidLayoutSubviews for example).


It might be easier to not fight the system and either go with all IB or all code (create different sets of constraints and install/uninstall them in viewWillTransitionToSize when the trait collection changes) solutions for now. It would certainly be nice to be able to access the constant values for different trait collections in code... feel free to file an enhancement request 🙂

For better understanding, could you describe the use case where you want to change a variation ?


This confirms junkpile answer and may interest you

h ttps://stackoverflow.com/questions/35601518/how-do-i-programmatically-change-the-size-class-variation-on-an-nslayoutconstrai

Programme Constraint Variation
 
 
Q