1. <tfoot id='vd09Y'></tfoot>

      2. <legend id='vd09Y'><style id='vd09Y'><dir id='vd09Y'><q id='vd09Y'></q></dir></style></legend>

          <bdo id='vd09Y'></bdo><ul id='vd09Y'></ul>
        <i id='vd09Y'><tr id='vd09Y'><dt id='vd09Y'><q id='vd09Y'><span id='vd09Y'><b id='vd09Y'><form id='vd09Y'><ins id='vd09Y'></ins><ul id='vd09Y'></ul><sub id='vd09Y'></sub></form><legend id='vd09Y'></legend><bdo id='vd09Y'><pre id='vd09Y'><center id='vd09Y'></center></pre></bdo></b><th id='vd09Y'></th></span></q></dt></tr></i><div id='vd09Y'><tfoot id='vd09Y'></tfoot><dl id='vd09Y'><fieldset id='vd09Y'></fieldset></dl></div>

        <small id='vd09Y'></small><noframes id='vd09Y'>

        如何使用自动布局使用其超级视图调整方形视图的大小

        时间:2023-09-10
        <i id='Otqf0'><tr id='Otqf0'><dt id='Otqf0'><q id='Otqf0'><span id='Otqf0'><b id='Otqf0'><form id='Otqf0'><ins id='Otqf0'></ins><ul id='Otqf0'></ul><sub id='Otqf0'></sub></form><legend id='Otqf0'></legend><bdo id='Otqf0'><pre id='Otqf0'><center id='Otqf0'></center></pre></bdo></b><th id='Otqf0'></th></span></q></dt></tr></i><div id='Otqf0'><tfoot id='Otqf0'></tfoot><dl id='Otqf0'><fieldset id='Otqf0'></fieldset></dl></div>
        <legend id='Otqf0'><style id='Otqf0'><dir id='Otqf0'><q id='Otqf0'></q></dir></style></legend>
        • <small id='Otqf0'></small><noframes id='Otqf0'>

              <tbody id='Otqf0'></tbody>
              <bdo id='Otqf0'></bdo><ul id='Otqf0'></ul>

                  <tfoot id='Otqf0'></tfoot>
                1. 本文介绍了如何使用自动布局使用其超级视图调整方形视图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我的特殊情况下,我在视图控制器中有视图,我为其添加了以下约束:

                  In my particular case I have view inside the view controller for which I added the following constraints:

                  • 将前缘、后缘、顶边和底边设置为 0
                  • 将底边的乘数设置为 2:1

                  现在视图位于视图控制器的上半部分.

                  Now the view sits in the top half on the view controller.

                  在其中我添加了一个方形图像视图,为此我添加了以下约束:

                  Inside this I add a square image view, for which I added the following constraints:

                  • Ctrl 从图像视图拖到超级视图并添加相等的高度和宽度.
                  • 更改宽度和高度的乘数,直到我得到一个完美的正方形.
                  • 添加了垂直和水平居中的约束

                  我的约束看起来很完美,但是在模拟器中运行时,我没有得到一个完美的正方形.除此之外,在不同的模拟器屏幕上运行时,图像视图不会调整大小.

                  My constraints seems perfect, but when running in the simulator I don't get a perfect square. Besides this, the image view doesn't get resize when running on different simulator screens.

                  这是我的设置:

                  • 已启用自动布局和尺寸类别
                  • 我为情节提要使用推断大小
                  • 自适应布局设置为任意宽度和高度
                  • 我正在尝试为 4s、5、6 和 6+ 模拟器运行此程序.

                  我查看了其他 stackoverflow 帖子,但似乎没有任何效果.

                  I looked on other stackoverflow posts, but nothing seems to work.

                  是否有一些基本步骤可以做到这一点?

                  Are there some basic steps to do this ?

                  设置>=10个约束后:

                  After setting >=10 constraints:

                  我添加了顶部,底部,前导,尾随约束2次,1次小于或等于(优先级1000),另一个大于或等于(优先级800),常数值为90.我不知道为什么底部它试图从主视图延伸到 90 pt,而不是容器视图(绿色).

                  edit 3: I added top,bottom,leading,trailing constraints 2 times, 1 with lower than or equal(priority 1000), the other one with greater than or equal(priority 800) with the constant value of 90. I don't know why for bottom it tries to streches to 90 pt from the main view, not the container view(green one).

                  推荐答案

                  您有一个视图需要扩展以填充其容器,同时保持其纵横比.这是 Auto Layout 的常见模式.

                  You have a view that needs to expand to fill its container while maintaining its aspect ratio. This is a common pattern with Auto Layout.

                  诀窍是对前导/尾随/顶部/底部使用两个约束:

                  The trick is to use two constraints for leading/trailing/top/bottom:

                  • =10 低优先级

                  >=10 在所需的优先级.

                  综合起来,你有:

                  • 宽高比 1:1

                  • Aspect Ratio 1:1

                  在 Superview 中居中 X/Y

                  Center X/Y in Superview

                  前导/尾随/顶部/底部到超级视图 = 10(优先级为 750)

                  Leading/Trailing/Top/Bottom to Superview = 10 (at 750 priority)

                  前导/尾随/顶部/底部到 Superview >= 10(优先级为 1000)

                  Leading/Trailing/Top/Bottom to Superview >= 10 (at 1000 priority)

                  UIImageView 还需要考虑一些事项:

                  There are also a couple of things to consider with UIImageView:

                  • UIImageView 将根据其显示的图像具有固有的内容大小,因此您需要确保其 Content Hugging Priority 低于您用于 =10<的 750 优先级/code> 约束.

                  • UIImageView will have an intrinsic content size based on the image that it is displaying, so you'll want to ensure that its Content Hugging Priority is lower than the 750 priority you use for the =10 constraints.

                  UIImageView.contentMode 确定基础图像的大小相对于 UIImageView 的大小.默认情况下,它设置为 UIViewContentModeScaleToFill.

                  UIImageView.contentMode determines how the underlying image is sized relative to the size of the UIImageView. By default, it's set to UIViewContentModeScaleToFill.

                  这篇关于如何使用自动布局使用其超级视图调整方形视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  • <bdo id='vj2X7'></bdo><ul id='vj2X7'></ul>
                    <legend id='vj2X7'><style id='vj2X7'><dir id='vj2X7'><q id='vj2X7'></q></dir></style></legend>

                    <i id='vj2X7'><tr id='vj2X7'><dt id='vj2X7'><q id='vj2X7'><span id='vj2X7'><b id='vj2X7'><form id='vj2X7'><ins id='vj2X7'></ins><ul id='vj2X7'></ul><sub id='vj2X7'></sub></form><legend id='vj2X7'></legend><bdo id='vj2X7'><pre id='vj2X7'><center id='vj2X7'></center></pre></bdo></b><th id='vj2X7'></th></span></q></dt></tr></i><div id='vj2X7'><tfoot id='vj2X7'></tfoot><dl id='vj2X7'><fieldset id='vj2X7'></fieldset></dl></div>
                        <tbody id='vj2X7'></tbody>
                    1. <tfoot id='vj2X7'></tfoot>
                      • <small id='vj2X7'></small><noframes id='vj2X7'>