菜鸟教程小白 发表于 2022-12-11 19:49:23

ios - UITableViewCell中的自动布局约束不匹配


                                            <p><p>为动态单元格高度创建简单的应用程序。</p>

<p>我有自定义 <code>UITableViewCell</code> 并且我有一些控件。我已经修复了所有控件的约束。约束集后 Tableview 不显示任何错误。 Tableview 运行时单元格高度没问题,但是当我检查控制台日志时,它给了我如下所示的错误。</p>

<pre><code>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.
2017-06-08 17:54:22.184177+0530 Test 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.
(
    &#34;&lt;NSLayoutConstraint:0x600000286680 UILabel:0x7fe86bd1c4e0&#39;Label&#39;.height == 50   (active)&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x600000286810 V:|-(14)-   (active, names: &#39;|&#39;:UITableViewCellContentView:0x7fe86bd1c220 )&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x600000286860 V:-(15)-|   (active, names: &#39;|&#39;:UITableViewCellContentView:0x7fe86bd1c220 )&gt;&#34;,
    &#34;&lt;NSLayoutConstraint:0x600000286cc0 &#39;UIView-Encapsulated-Layout-Height&#39; UITableViewCellContentView:0x7fe86bd1c220.height == 49   (active)&gt;&#34;
)

Will attempt to recover by breaking constraint
&lt;NSLayoutConstraint:0x600000286680 UILabel:0x7fe86bd1c4e0&#39;Label&#39;.height == 50   (active)&gt;
</code></pre>

<p><strong>Bellow 是我的 TableView 及其约束。</strong>
<a href="/image/9kTpQ.png" rel="noreferrer noopener nofollow"><img src="/image/9kTpQ.png" alt="enter image description here"/></a> </p>

<p><strong>下面是我的 TableView 代码</strong></p>

<pre><code>- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = ;
    NSString * strAction = arrActionList;
    if () {
      cell.lblActionHeight.constant = 50;
    }
    else{
      cell.lblActionHeight.constant = 20;
    }
    cell.lblAction.text = strAction;
    ;
    return cell;
}
</code></pre>

<p>提前致谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果你不想给出高度限制,你应该给出顶部、底部、前导、尾随。</p>

<p>如果你需要高度限制,那么你应该给 (leading,trailing,top,height) 或 (leading,trailing,centerY,height)</p>

<p>确保在 heightForRow 方法中返回正确的高度。</p>

<p>如果你想使用<code>UITableViewAutomaticDimension</code></p>,你需要指定估计的行高

<p>在您的情况下,estimatedRow 高度是 IB 中的确切行高。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableViewCell中的自动布局约束不匹配,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44436336/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44436336/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableViewCell中的自动布局约束不匹配