菜鸟教程小白 发表于 2022-12-13 03:32:36

ios - 如何禁用单个 UI 元素的自动布局(UIlabel)


                                            <p><p>我需要更改使用自动布局的 Viewcontroller 中标签的位置。在此之前,我使用了 </p>

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

<p>但这会产生一些运行时警告</p>

<pre><code> Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in &lt;UIKit/UIView.h&gt; may also be helpful.
2013-08-15 23:25:58.791 Vicinitime Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don&#39;t want. Try this: (1) look at each constraint and try to figure out which you don&#39;t expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you&#39;re seeing NSAutoresizingMaskLayoutConstraints that you don&#39;t understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
    &#34;&lt;NSLayoutConstraint:0x9173a20 UIScrollView:0x9580190.bottom == UIView:0x9173890.bottom&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x9171fe0 V:|-(138)-   (Names: &#39;|&#39;:UIScrollView:0x9580190 )&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x91739a0 V:|-(0)-   (Names: &#39;|&#39;:UIView:0x9173890 )&gt;&#34;,
    &#34;&lt;NSAutoresizingMaskLayoutConstraint:0xab6ec90 h=--&amp; v=&amp;-&amp; UILabel:0x958cf70.midY == -6.3989*UIScrollView:0x9580190.height&gt;&#34;,
    &#34;&lt;NSAutoresizingMaskLayoutConstraint:0xab6eef0 h=--&amp; v=&amp;-&amp; V:&gt;&#34;,
    &#34;&lt;NSAutoresizingMaskLayoutConstraint:0xab6dbd0 h=--&amp; v=--&amp; V:&gt;&#34;
)

Will attempt to recover by breaking constraint
&lt;NSLayoutConstraint:0x9171fe0 V:|-(138)-   (Names: &#39;|&#39;:UIScrollView:0x9580190 )&gt;
</code></pre>

<p>所以我环顾四周,发现要解决我必须设置的问题
      ;
      ;</p>

<p>没有。问题是现在标签没有以编程方式改变位置。</p>

<p>如果您需要编写代码以编程方式设置标签:</p>

<pre><code>    if(travelTimeInt &lt;= activityTimeInt){

       forState:UIControlStateNormal];
      NSLog(@&#34;got to setting label place&#34;);

      ;

      ;



    }
    if(travelTimeInt * 7 &gt;= activityTimeInt){

             forState:UIControlStateNormal];

          NSLog(@&#34;got to setting label place&#34;);

            self.arriveTimeLabel.frame = CGRectMake(71, 170, 41, 25);

       self.departTimeLabel.frame = CGRectMake(207, 170, 41, 25);

    }
</code></pre>

<p>那么有没有办法在开启自动布局的情况下改变 uilabel 的位置?也许禁用自动布局?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果您使用的是自动布局,那么您的 <code>UIViews</code> 将根据它们的 <code>NSLayoutConstraints</code> 进行定位。</p>

<p>要移动它们,而不是设置它们的 <code>frames</code>,您需要更改约束。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何禁用单个 UI 元素的自动布局(UIlabel),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/18267111/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/18267111/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何禁用单个 UI 元素的自动布局(UIlabel)