Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode6: Is there any easy way to copy constrains between size classes?

I setup constrains in wAny hAny size class, and it worked on iPhone & iPad simulators.

wAny hAny

Then I switched to wAny hCompact size class and made some changes

wAny hCompact

It looks good in iPhone landscape mode, but change nothing in iPad, because iPad is wRegular hRegular. I try to apply these constrains to wRegular hAny size class, but can't find a quick way to do that. It makes no sense to manually modify all the constrains again. I found that if I double click a constrain in Inspector panel, there's a option to install the constrain to other size classes. But this is only for a single constrain...

inspector

So, is it possible to apply all the constraints from one size class to another?

like image 881
Ken Zhang Avatar asked Sep 07 '14 02:09

Ken Zhang


2 Answers

Select the Constrains on the left panel.(Select all the constraints). Then tap on Attribute Inspector on the right panel. Then add the class you want to add like how we add the UI elements.

Ref Image

like image 31
sasi kumar Avatar answered Nov 04 '22 10:11

sasi kumar


Thought i'd post this even though Hannes answer was accepted as it might be of use to somebody. You can bulk update the size classes for your constraints by editing the source code directly. Generally a bad idea to edit IB generated XML directly but I have performed an update using good old find and replace and it's saved me a lot of time.

Obvious disclaimer: make a backup of the file in case something goes horribly wrong ...

Look for the nodes like:

<variation key="heightClass=(Size Class)-widthClass=(Size Class)">

In my case I wanted to transplant all my 'Compact Width/Compact Height' constraints to 'Any Width/Compact Height', and was able to achieve this by doing a find/replace of '-widthClass=compact' with an empty string to change 'heightClass=compact-widthClass=compact' to just "heightClass=compact".

To get to source code view right click the storyboard file in xcode and select 'Open As > Source Code'

Pretty straightforward in this instance and a lot easier than doing it in IB as I had lots of constraints to update.

like image 128
SteveOLeary Avatar answered Nov 04 '22 09:11

SteveOLeary