<small id='x3Z2Y'></small><noframes id='x3Z2Y'>

  • <i id='x3Z2Y'><tr id='x3Z2Y'><dt id='x3Z2Y'><q id='x3Z2Y'><span id='x3Z2Y'><b id='x3Z2Y'><form id='x3Z2Y'><ins id='x3Z2Y'></ins><ul id='x3Z2Y'></ul><sub id='x3Z2Y'></sub></form><legend id='x3Z2Y'></legend><bdo id='x3Z2Y'><pre id='x3Z2Y'><center id='x3Z2Y'></center></pre></bdo></b><th id='x3Z2Y'></th></span></q></dt></tr></i><div id='x3Z2Y'><tfoot id='x3Z2Y'></tfoot><dl id='x3Z2Y'><fieldset id='x3Z2Y'></fieldset></dl></div>

      1. <legend id='x3Z2Y'><style id='x3Z2Y'><dir id='x3Z2Y'><q id='x3Z2Y'></q></dir></style></legend>
          <bdo id='x3Z2Y'></bdo><ul id='x3Z2Y'></ul>
        <tfoot id='x3Z2Y'></tfoot>

      2. 调用 layoutIfNeeded() 时 AutoLayout 会破坏约束

        时间:2023-09-10
      3. <legend id='ypJ5h'><style id='ypJ5h'><dir id='ypJ5h'><q id='ypJ5h'></q></dir></style></legend>

        <small id='ypJ5h'></small><noframes id='ypJ5h'>

        <tfoot id='ypJ5h'></tfoot>

        <i id='ypJ5h'><tr id='ypJ5h'><dt id='ypJ5h'><q id='ypJ5h'><span id='ypJ5h'><b id='ypJ5h'><form id='ypJ5h'><ins id='ypJ5h'></ins><ul id='ypJ5h'></ul><sub id='ypJ5h'></sub></form><legend id='ypJ5h'></legend><bdo id='ypJ5h'><pre id='ypJ5h'><center id='ypJ5h'></center></pre></bdo></b><th id='ypJ5h'></th></span></q></dt></tr></i><div id='ypJ5h'><tfoot id='ypJ5h'></tfoot><dl id='ypJ5h'><fieldset id='ypJ5h'></fieldset></dl></div>
          <bdo id='ypJ5h'></bdo><ul id='ypJ5h'></ul>
                <tbody id='ypJ5h'></tbody>

                1. 本文介绍了调用 layoutIfNeeded() 时 AutoLayout 会破坏约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Swift 使用 XCode6 在 UITableView 中实现动态高度 UITaleViewCell.

                  I'm trying to implement a dynamic height UITaleViewCell in UITableView with XCode6 using Swift.

                  通过以图形方式设置约束,我将单元格布局如下(屏幕截图来自 XCode5,因为 XCode6 上的 NDA).我还将 BodyLabel 的 Line Break 属性设置为自动换行",并将行号设置为0"以允许多行.

                  I laid out my cell as following, by setting up the constraints graphically(Screenshot is from XCode5, because of NDA on XCode6). I also set the BodyLabel's Line Break property to 'Word Wrap', and set the Line number to '0' to allow multiple lines.

                  现在,如果我只是在 tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) 方法中设置单元格的内容,那么我可以正确获得动态高度行为.

                  Now if I just set up the cell's contents inside tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) method, then I get the dynamic height behavior correctly.

                  但是,由于我正在关注在线可用的教程(特别是 这个),我添加了另一种方法来确定单元格的高度 tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!).在我关注的教程中,它告诉我添加 cell.layoutIfNeeded(),所以我也添加了它.

                  However, since I was following along tutorials available online(specifically this one), I added another method to determine the height of the cell with tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!). In the tutorial that I was following, it told me to add cell.layoutIfNeeded(), so I added that too.

                  override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
                  
                      var cell = tableView!.dequeueReusableCellWithIdentifier(kCellIdentifier) as GroupFeedCell
                  
                      // Configure the cell
                      if let frc = self.fetchedResultsController {
                          let feed = frc.objectAtIndexPath(indexPath) as GroupFeed
                          cell.titleLabel.text = feed.name
                          if let message = feed.message {
                              cell.bodyLabel.text = message
                          }
                      }
                  
                      // Layout the cell
                      cell.layoutIfNeeded()
                  
                      // Get the height
                      var height : CGFloat = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
                      return height
                  }
                  

                  但是,当我运行程序时,虽然表格视图仍然正确显示单元格的动态高度,但它显示的错误如下:

                  However, when I ran the program, although the table view still displayed the dynamic heights of the cells correctly, it displayed the errors like this:

                  2014-07-27 13:59:22.599 FBGroups[4631:979424] Unable to simultaneously satisfy constraints.
                      Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each    constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or    constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer    to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
                  (
                      "<NSLayoutConstraint:0x17809e780 H:[UILabel:0x14fd12f50'Body Label Contents...']-(8)-|   (Names:    '|':UITableViewCellContentView:0x178185d70 )>",
                      "<NSLayoutConstraint:0x17809e7d0 H:|-(8)-[UILabel:0x14fd12f50'Body Label Contents...']   (Names:    '|':UITableViewCellContentView:0x178185d70 )>",
                      "<NSLayoutConstraint:0x17809ea50 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x178185d70(0)]>"
                  )
                  
                  Will attempt to recover by breaking constraint 
                  <NSLayoutConstraint:0x17809e780 H:[UILabel:0x14fd12f50'Body Label Contents...']-(8)-|   (Names:     '|':UITableViewCellContentView:0x178185d70 )>
                  
                  Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
                  The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.  
                  

                  我试图找出可能出了什么问题,并在上面花费了大量时间,我发现每当我删除 cell.layoutIfNeeded() 方法时,约束错误就会消失.

                  I tried to figure out what might have gone wrong, and spend good amount of time on it, and I figured out that whenever I delete cell.layoutIfNeeded() method, then the constraints error disappears.

                  似乎在冲突的约束中,UIView-Encapsulated-Layout-Width 不是我添加的,除此之外,所有约束对我来说都是无辜的.我试图搜索可以生成 UIView-Encapsulated-Layout-Width 约束的内容,但在我的情况下无法得到令人满意的解释.我想知道此错误消息的原因以及如何解决此问题.

                  And it seemed that among the constraints that are conflicting, UIView-Encapsulated-Layout-Width was not the one I added, and other than that, the all of the constraints looked innocent to me. I tried to search through what could generate UIView-Encapsulated-Layout-Width constraints, but I couldn't get the satisfying explanation in my situation. I would like to know what would be the cause of this error message and how to resolve this problem.

                  另外,有人可以解释在计算单元格高度时调用 cell.layoutIfNeeded() 方法的目的是什么,以及何时需要?大多数涉及动态高度 UITableViewCell 的教程都使用了这种方法,尽管我的程序在没有该方法调用的情况下仍能正确显示单元格,并且每当我尝试使用该方法时,都会导致异常.

                  Also, can someone explain what would be the purpose of calling cell.layoutIfNeeded() method inside calculating height of the cell, and when it would be necessary? Most of the tutorials that were covering dynamic height UITableViewCell utilized this method, although my program still displayed cells correctly without that method call, and whenever I tried to use that method, it caused the exceptions.

                  推荐答案

                  在 Xcode 中,将最底部垂直约束的优先级设置为 750(或小于 1000 的任何值).

                  In Xcode, set the priority of the very bottom vertical constraint to 750 (or anything less than a 1000).

                  这篇关于调用 layoutIfNeeded() 时 AutoLayout 会破坏约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:自 IOS 8 以来使用自动布局和滚动视图的额外顶部空白 下一篇:设置 UIView 大小以适应子视图

                  相关文章

                  <small id='95814'></small><noframes id='95814'>

                    <tfoot id='95814'></tfoot>
                      <i id='95814'><tr id='95814'><dt id='95814'><q id='95814'><span id='95814'><b id='95814'><form id='95814'><ins id='95814'></ins><ul id='95814'></ul><sub id='95814'></sub></form><legend id='95814'></legend><bdo id='95814'><pre id='95814'><center id='95814'></center></pre></bdo></b><th id='95814'></th></span></q></dt></tr></i><div id='95814'><tfoot id='95814'></tfoot><dl id='95814'><fieldset id='95814'></fieldset></dl></div>

                      • <bdo id='95814'></bdo><ul id='95814'></ul>
                      <legend id='95814'><style id='95814'><dir id='95814'><q id='95814'></q></dir></style></legend>