菜鸟教程小白 发表于 2022-12-13 09:49:13

ios - 以编程方式创建的 UIView 的中心 X 到另一个 UIView - Objective C


                                            <p><p>只是为了尝试一下,我以编程方式创建了一个按钮,如下所示</p>

<pre><code>UIBUtton *button = ;
;
;
;
</code></pre>

<p>我的 View 中已经有一个名为 _teSTLabel 的现有标签。我正在使用自动布局,我想将我新添加的按钮的 X 居中到 _teSTLabel,我尽我所能尝试了下面的代码:</p>

<pre><code>[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:_testlabel attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual toItem:button
attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
</code></pre>

<p>我所能做的就是将_teSTLabel 的X 放在新添加的按钮上,而不是相反。我在这里错过了什么?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可能需要对按钮和标签执行此操作:</p>

<pre><code>    ;
    ;
</code></pre>

<p>然后使用 AutoLayout 约束格式化语言添加额外的调整大小约束:</p>

<pre><code>;
</code></pre>

<p>和/或还使用更多</p>

<pre><code>
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 以编程方式创建的 UIView 的中心 X 到另一个 UIView - Objective C,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/31905716/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/31905716/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 以编程方式创建的 UIView 的中心 X 到另一个 UIView - Objective C