菜鸟教程小白 发表于 2022-12-12 10:22:52

IOS 简单的 Visual Format 布局不起作用


                                            <p><p>我正在使用 Objective-C 和 XCode 7 练习可视化格式布局,但很难让一个非常简单的布局工作。
我的应用程序只是一个简单的单 View 应用程序,我正在尝试将 UILabel 添加到主视图。
下面是代码。
</p>

<pre><code>- (void)viewDidLoad {
    ;
    // Do any additional setup after loading the view, typically from a nib.

    UILabel *label = [ init];
    label.text = @&#34;Hello world&#34;;
    label.layer.borderWidth = 1.0f;
    label.layer.borderColor = .CGColor;

    ;

    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(label);
    [self.view addConstraints:
   |&#34;
                                             options:0
                                             metrics:nil views:viewsDictionary]];
    [self.view addConstraints:
   |&#34;
                                             options:0
                                             metrics:nil views:viewsDictionary]];

    NSLog(@&#34;constraints: %@&#34;, self.view.constraints);
}
</code></pre>

<p>运行这个我得到一堆错误:</p>

<pre><code>    2016-02-01 18:14:15.729 AutolayoutVisualFormat constraints: (
    &#34;&lt;_UILayoutSupportConstraint:0x79771ab0 V:&gt;&#34;,
    &#34;&lt;_UILayoutSupportConstraint:0x79786d30 V:|-(0)-   (Names: &#39;|&#39;:UIView:0x79771790 )&gt;&#34;,
    &#34;&lt;_UILayoutSupportConstraint:0x797884e0 V:&gt;&#34;,
    &#34;&lt;_UILayoutSupportConstraint:0x79777540 _UILayoutGuide:0x79772e30.bottom == UIView:0x79771790.bottom&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x7978d930 H:|-(0)-   (Names: &#39;|&#39;:UIView:0x79771790 )&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x7978da50 H:-(0)-|   (Names: &#39;|&#39;:UIView:0x79771790 )&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x7978fcb0 V:|-(0)-   (Names: &#39;|&#39;:UIView:0x79771790 )&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x7978fce0 V:-(0)-|   (Names: &#39;|&#39;:UIView:0x79771790 )&gt;&#34;
)
2016-02-01 18:14:15.733 AutolayoutVisualFormat 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;NSAutoresizingMaskLayoutConstraint:0x7a0630a0 h=--&amp; v=--&amp; H:&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x7978d930 H:|-(0)-   (Names: &#39;|&#39;:UIView:0x79771790 )&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x7978da50 H:-(0)-|   (Names: &#39;|&#39;:UIView:0x79771790 )&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x7a080130 &#39;UIView-Encapsulated-Layout-Width&#39; H:&gt;&#34;
)

Will attempt to recover by breaking constraint
&lt;NSLayoutConstraint:0x7978da50 H:-(0)-|   (Names: &#39;|&#39;:UIView:0x79771790 )&gt;

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints 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.
2016-02-01 18:14:15.733 AutolayoutVisualFormat 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;NSAutoresizingMaskLayoutConstraint:0x7a0758b0 h=--&amp; v=--&amp; V:&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x7978fcb0 V:|-(0)-   (Names: &#39;|&#39;:UIView:0x79771790 )&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x7978fce0 V:-(0)-|   (Names: &#39;|&#39;:UIView:0x79771790 )&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x7a081610 &#39;UIView-Encapsulated-Layout-Height&#39; V:&gt;&#34;
)

Will attempt to recover by breaking constraint
&lt;NSLayoutConstraint:0x7978fce0 V:-(0)-|   (Names: &#39;|&#39;:UIView:0x79771790 )&gt;

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints 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.
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><ul>
<li>添加这一行就可以了</li>
</ul>

<blockquote>
<p>;</p>
</blockquote>

<ul>
<li>在代码中创建的 View 上使用自动布局时,有一些
需要注意的警告。第一个与值(value)有关
属性 <code>translatesAutoresizingMaskIntoConstraints</code>。该属性(property)是
默认为 YES,这意味着将创建 Auto Layout 约束
基于 View 的自动调整大小掩码。我们希望尊重的观点
我们将添加的自动布局约束,所以这个属性应该是
设置为 NO。</li>
<li>这是以前用于管理 View 大小的旧技术
你可以引用这个链接<a href="https://stackoverflow.com/questions/10468389/uiview-autoresizingmask-interface-builder-to-code-programmatically-create-st" rel="noreferrer noopener nofollow">UIView autoresizingMask - Interface Builder to Code - Programmatically create struts and springs - Swift or Objective-C</a> </li>
</ul>

<blockquote>
<ul>
<li>When this property is YES, which it is by default, the autoresizing    mask of a view is translated into constraints. For example, if a view
is configured as in Figure 6-1 and<br/>
translatesAutoresizingMaskIntoConstraints is YES, then the<br/>
constraints |-20--20-| and V:|-20- are added to<br/>
the view’s superview. The net effect is that unaware views behave as<br/>
they did in versions of OS X prior to 10.7.
   For views that are aware of Auto Layout, in most circumstances you    will want translatesAutoresizingMaskIntoConstraints to be NO.
This is    because the constraints generated by translating the
autoresizing    mask are already sufficient to completely specify the
frame of a view    given its superview’s frame, which is generally too
much. For    example, this will prevent a button from automatically
assuming its    optimal width when its title is changed.</li>
</ul>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于IOS 简单的 Visual Format 布局不起作用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/35129248/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/35129248/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: IOS 简单的 Visual Format 布局不起作用