是否可能将约束从一个视图复制到另一个视图?

时间:2022-12-27 10:21:27

Suppose I use Interface Builder to create UI in Storyboard with Auto Layout. Can I copy or move some constraints from one view to another?

假设我使用Interface Builder在Storyboard中创建带有自动布局的UI。我可以将某些约束从一个视图复制或移动到另一个视图吗?

3 个解决方案

#1


11  

If you are using interface builder, some constraints will be automatic copied if you use the cmd-c or edit/copy: the ones that include the copying view hierarchy. Otherwise, no, you can't. Copy the whole view if you want to preserve the constraints.

如果您使用的是接口构建器,那么如果您使用cmd-c或edit/copy(包含复制视图层次结构的那些),就会自动复制一些约束。否则,不,你不能。如果希望保留约束,请复制整个视图。

#2


14  

Here's my hack to get ALL the constraints to copy: I have a small view within my main view that I want to copy over to another view controller, in order to do this I copy over the entire main view into the new view controllers main view. I then drag my small view (on side hierarchy) into the main view of my new controller and then just deleted the old main view that I don't need. This way you keep all the constraints for the items within the small view.

下面是我复制所有约束的技巧:我在主视图中有一个小视图,我想将它复制到另一个视图控制器中,为了做到这一点,我将整个主视图复制到新的视图控制器主视图中。然后,我将我的小视图(在侧层次结构上)拖放到新控制器的主视图中,然后删除我不需要的旧主视图。这样,您就可以在小视图中保留项目的所有约束。

Hope this helps :)

希望这有助于:)

#3


9  

You can if you understand and learn how the XML of the .xib files works. I got pretty used to them and so I was able to move a view with its constraints into another view.

如果您理解并了解.xib文件的XML是如何工作的,就可以这样做。我已经习惯了它们,所以我可以把一个有约束的视图移动到另一个视图中。

I'll try to explain it step by step:

我会一步一步地解释:

  1. Create an outlet for it: myView
  2. 为它创建一个outlet: myView
  3. Right click the .xib file > Open As > Source Code or open it in another editor (e.g. Sublime Text)
  4. 右键单击。xib文件>作为>源代码打开,或者在另一个编辑器中打开(例如,崇高的文本)
  5. Search myView and you'll find something like:

    搜索myView,你会发现如下内容:

    <outlet property="myView" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
    

    and copy the destination attribute's value

    并复制目标属性的值

  6. Search the copied id (i5M-Pr-FkT) and one of the results will be a view tag:

    搜索复制的id (i5 - pr-fkt),其中一个结果将是一个视图标记:

    <view contentMode="scaleToFill" id="i5M-Pr-FkT"> <!-- 1 -->
        ...
    </view>
    
  7. Cut and paste this whole view tag in the needed view's subviews tag:

    在需要的视图的子视图标签中剪切并粘贴整个视图标记:

    <view contentMode="scaleToFill" id="Ovp-8Y-qHZ"> <!-- 2 -->
        <subviews>
            <view contentMode="scaleToFill" id="i5M-Pr-FkT"> <!-- 1 -->
                ...
            </view>
        </subviews>
    </view>
    
  8. Continue searching for the copied id and you'll find some constraints that have it like:

    继续搜索复制的id,您会发现一些约束,比如:

    <constraint firstItem="w7M-JQ-JWD" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leading" id="EwH-B1-XWY"/>
    
  9. You need to move this into the constraints tag of the lowest common ancestor of both superviews (the old one and the new one):

    您需要将其移动到两个超级视图(旧的和新视图)的最低共同祖先的约束标记中:

    <view contentMode="scaleToFill" id="rK2-sE-P0d"> <!-- 3 -->
        <subviews>
            <view contentMode="scaleToFill" id="Ovp-8Y-qHZ"> <!-- 2 -->
                <subviews>
                    <view contentMode="scaleToFill" id="i5M-Pr-FkT"> <!-- 1 -->
                        ...
                    </view>
                </subviews>
            </view>
        </subviews>
        <constraints>
            <constraint firstItem="w7M-JQ-JWD" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leading" id="EwH-B1-XWY"/>
        </constraints>
    </view>
    

#1


11  

If you are using interface builder, some constraints will be automatic copied if you use the cmd-c or edit/copy: the ones that include the copying view hierarchy. Otherwise, no, you can't. Copy the whole view if you want to preserve the constraints.

如果您使用的是接口构建器,那么如果您使用cmd-c或edit/copy(包含复制视图层次结构的那些),就会自动复制一些约束。否则,不,你不能。如果希望保留约束,请复制整个视图。

#2


14  

Here's my hack to get ALL the constraints to copy: I have a small view within my main view that I want to copy over to another view controller, in order to do this I copy over the entire main view into the new view controllers main view. I then drag my small view (on side hierarchy) into the main view of my new controller and then just deleted the old main view that I don't need. This way you keep all the constraints for the items within the small view.

下面是我复制所有约束的技巧:我在主视图中有一个小视图,我想将它复制到另一个视图控制器中,为了做到这一点,我将整个主视图复制到新的视图控制器主视图中。然后,我将我的小视图(在侧层次结构上)拖放到新控制器的主视图中,然后删除我不需要的旧主视图。这样,您就可以在小视图中保留项目的所有约束。

Hope this helps :)

希望这有助于:)

#3


9  

You can if you understand and learn how the XML of the .xib files works. I got pretty used to them and so I was able to move a view with its constraints into another view.

如果您理解并了解.xib文件的XML是如何工作的,就可以这样做。我已经习惯了它们,所以我可以把一个有约束的视图移动到另一个视图中。

I'll try to explain it step by step:

我会一步一步地解释:

  1. Create an outlet for it: myView
  2. 为它创建一个outlet: myView
  3. Right click the .xib file > Open As > Source Code or open it in another editor (e.g. Sublime Text)
  4. 右键单击。xib文件>作为>源代码打开,或者在另一个编辑器中打开(例如,崇高的文本)
  5. Search myView and you'll find something like:

    搜索myView,你会发现如下内容:

    <outlet property="myView" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
    

    and copy the destination attribute's value

    并复制目标属性的值

  6. Search the copied id (i5M-Pr-FkT) and one of the results will be a view tag:

    搜索复制的id (i5 - pr-fkt),其中一个结果将是一个视图标记:

    <view contentMode="scaleToFill" id="i5M-Pr-FkT"> <!-- 1 -->
        ...
    </view>
    
  7. Cut and paste this whole view tag in the needed view's subviews tag:

    在需要的视图的子视图标签中剪切并粘贴整个视图标记:

    <view contentMode="scaleToFill" id="Ovp-8Y-qHZ"> <!-- 2 -->
        <subviews>
            <view contentMode="scaleToFill" id="i5M-Pr-FkT"> <!-- 1 -->
                ...
            </view>
        </subviews>
    </view>
    
  8. Continue searching for the copied id and you'll find some constraints that have it like:

    继续搜索复制的id,您会发现一些约束,比如:

    <constraint firstItem="w7M-JQ-JWD" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leading" id="EwH-B1-XWY"/>
    
  9. You need to move this into the constraints tag of the lowest common ancestor of both superviews (the old one and the new one):

    您需要将其移动到两个超级视图(旧的和新视图)的最低共同祖先的约束标记中:

    <view contentMode="scaleToFill" id="rK2-sE-P0d"> <!-- 3 -->
        <subviews>
            <view contentMode="scaleToFill" id="Ovp-8Y-qHZ"> <!-- 2 -->
                <subviews>
                    <view contentMode="scaleToFill" id="i5M-Pr-FkT"> <!-- 1 -->
                        ...
                    </view>
                </subviews>
            </view>
        </subviews>
        <constraints>
            <constraint firstItem="w7M-JQ-JWD" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leading" id="EwH-B1-XWY"/>
        </constraints>
    </view>