自动版式打破约束时,layoutIfNeeded()被调用 [英] AutoLayout breaks constraints when layoutIfNeeded() is called

查看:475
本文介绍了自动版式打破约束时,layoutIfNeeded()被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个动态的高度 UITaleViewCell 的UITableView 使用雨燕X $ C $ 6。

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

我把我的电池如下,通过图形化设置的限制(截图是从X code5,因为NDA对X code6)。我还设置了BodyLabel的线路中断属性为自动换行,和行号设置为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的封装,布局宽度是不是我补充说,和其他重要的是,所有的看着制约无辜的我。我试图通过这可能产生 UIView的封装,布局宽度限制搜索,但我不能让我的情况令人满意的解释。我想知道什么是此错误信息的原因以及如何解决这个问题。

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.

推荐答案

在X code,最底层垂直约束的优先级设置为750(或比1000东西少)。

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

这篇关于自动版式打破约束时,layoutIfNeeded()被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆