Xcode6: Is there any easy way to copy constrains b

2019-03-28 20:17发布

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?

4条回答
时光不老,我们不散
2楼-- · 2019-03-28 20:29

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

查看更多
女痞
3楼-- · 2019-03-28 20:31

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.

查看更多
淡お忘
4楼-- · 2019-03-28 20:37

Find and replace story board code: I did this to migrate from compact-regular to any/any . Works for me . Find - heightClass=regular-widthClass=compact Replace - default

For other size clasees find the size string appropriately and resize. Kudos!!

Update constraints at the end. Else everything would be not as expected.

查看更多
戒情不戒烟
5楼-- · 2019-03-28 20:45

No, you cannot edit all the constraints together.

But for each, you simply click that plus button in front of Installed and add wAny hAny and select that. Then your constraint will apply to all size classes. You can add more options and thus, decide when constraints apply.

查看更多
登录 后发表回答