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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28625035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:55:06  来源:igfitidea点击:

Is it possible to copy constraints from one view to another?

iosxcodemacosstoryboard

提问by kelin

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。我可以将一些约束从一个视图复制或移动到另一个视图吗?

采纳答案by catanore

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 或编辑/复制,一些约束将被自动复制:包括复制视图层次结构的约束。否则,不,你不能。如果要保留约束,请复制整个视图。

回答by Trianna Brannon

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 :)

希望这可以帮助 :)

回答by Iulian Onofrei

You can if you understand and learn how the XML of the .xibfiles 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. Right click the .xibfile > Open As> Source Codeor open it in another editor (e.g. Sublime Text)
  3. Search myViewand you'll find something like:

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

    and copy the destinationattribute's value

  4. Search the copied id (i5M-Pr-FkT) and one of the results will be a viewtag:

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

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

    <constraint firstItem="w7M-JQ-JWD" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leading" id="EwH-B1-XWY"/>
    
  7. You need to move this into the constraintstag 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. 为它创建一个出口: myView
  2. 右键单击.xib文件> Open As>Source Code或在另一个编辑器中打开它(如Sublime Text
  3. 搜索myView,你会发现类似的东西:

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

    并复制destination属性的值

  4. 搜索复制的 id ( i5M-Pr-FkT),结果之一将是一个view标签:

    <view contentMode="scaleToFill" id="i5M-Pr-FkT"> <!-- 1 -->
        ...
    </view>
    
  5. 将整个view标签剪切并粘贴到所需视图的subviews标签中:

    <view contentMode="scaleToFill" id="Ovp-8Y-qHZ"> <!-- 2 -->
        <subviews>
            <view contentMode="scaleToFill" id="i5M-Pr-FkT"> <!-- 1 -->
                ...
            </view>
        </subviews>
    </view>
    
  6. 继续搜索复制的 id,你会发现一些约束,比如:

    <constraint firstItem="w7M-JQ-JWD" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leading" id="EwH-B1-XWY"/>
    
  7. 您需要将其移动到constraints两个超级视图(旧的和新的)的最低共同祖先的标签中:

    <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>