菜鸟教程小白 发表于 2022-12-11 17:24:03

ios - 自动布局以放置 4 个具有相同宽度的 View


                                            <p><p> <a href="/image/NiWeC.png" rel="noreferrer noopener nofollow"><img src="/image/NiWeC.png" alt="enter image description here"/></a> </p>

<p>我正在使用自动布局。我想将 View 高度设置为等于另一个 View 的宽度。棕色 View 的宽度设置为与其父 View 成比例的 0.3,绿色 View 的高度应与棕色 View 的宽度相同。
我已将约束设置为 4 个 View ,但 4 个 View 的宽度不一样</p>

<p>请引用图片。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><strong>Obj-c</strong></p>

<pre><code>NSLayoutConstraint *constraint = [NSLayoutConstraint
    constraintWithItem:greenView
    attribute:NSLayoutAttributeHeight
    relatedBy:NSLayoutRelationEqual
    toItem:brownView
    attribute:NSLayoutAttributeWidth
    multiplier: 1
    constant:0];
</code></pre>

<p><strong> swift </strong></p>

<pre><code> NSLayoutConstraint(item: greenView,
                  attribute: .Height,
                  relatedBy: .Equal,
                  toItem: brownView,
                  attribute: .Width,
                  multiplier: 1,
                  constant: 0.0);
</code></pre>

<p><em>我没有测试代码,如果有问题请告诉我</em></p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 自动布局以放置 4 个具有相同宽度的 View ,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39204245/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39204245/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 自动布局以放置 4 个具有相同宽度的 View