我使用NSLayoutConstraint
代码自动布局我的视图以设置我的视图高度,我有一个奇怪的错误,如下所示:
当我使用fraction number
324.867
来设置我的视图高度时。它引发错误Unable to simultaneously satisfy constraints
。
但如果我将324.867
转为324
。错误消失了。
任何人都能解释为什么会这样吗?
这是我的代码:
let estimateSizeOfTopic = CGSize(width: self.frame.width - 10.0, height: 1000.0)
let attributeOfTopic = [NSFontAttributeName: UIFont.systemFont(ofSize: 14)]
let estimateHeight = NSString(string: topicTitle.text!).boundingRect(with: estimateSizeOfTopic, options: .usesLineFragmentOrigin, attributes: attributeOfTopic, context: nil).height
NSLayoutConstraint(item: topicTitle, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: estimateHeight).isActive = true
NSLayoutConstraint(item: topicTitle, attribute: .top, relatedBy: .equal, toItem: superView, attribute: .top, multiplier: 1.0, constant: 5.0).isActive = true
NSLayoutConstraint(item: topicTitle, attribute: .left, relatedBy: .equal, toItem: superView, attribute: .left, multiplier: 1.0, constant: 5.0).isActive = true
NSLayoutConstraint(item: topicTitle, attribute: .right, relatedBy: .equal, toItem: superView, attribute: .right, multiplier: 1.0, constant: -5.0).isActive = true
NSLayoutConstraint(item: topicTitle, attribute: .bottom, relatedBy: .equal, toItem: superView, attribute: .bottom, multiplier: 1.0, constant: -5.0).isActive = true
//我的superView对它没有任何约束......我编码的方式是因为我想通过我的topicTitle高度自动设置我的superView的高度(表视图单元格自动调整大小)
错误发生在第一个约束。
//If I use this line of code. It raise an error
NSLayoutConstraint(item: topicTitle, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: estimateHeight).isActive = true
//If I use this line of code instead. The error is gone
NSLayoutConstraint(item: topicTitle, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: round(estimateHeight)).isActive = true
我对estimateHeight
进行了舍入,因为如果我不绕它,则会引发错误。
但是有一个错误的启发。我认为的每件事情看起来都是正确的
这就是调试窗口所说的:
[LayoutConstraints] 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.
(
"<NSLayoutConstraint:0x608000484560 UITextView:0x7f9049080c00'It is a long established ...'.height == 324.867 (active)>",
"<NSLayoutConstraint:0x608000285280 V:|-(5)-[UITextView:0x7f9049080c00'It is a long established ...'] (active, names: '|':SatacusWorld.PostViewCell:0x7f9049096a00'postIdentifier' )>",
"<NSLayoutConstraint:0x60800029fe50 UITextView:0x7f9049080c00'It is a long established ...'.bottom == SatacusWorld.PostViewCell:0x7f9049096a00'postIdentifier'.bottom - 5 (active)>",
"<NSLayoutConstraint:0x60800029fcc0 'UIView-Encapsulated-Layout-Height' SatacusWorld.PostViewCell:0x7f9049096a00'postIdentifier'.height == 334.8 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x608000484560 UITextView:0x7f9049080c00'It is a long established ...'.height == 324.867 (active)>
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.
答案 0 :(得分:0)
这绝对是一个奇怪的错误,因为这些错误中的数学加起来(字面上)。
然而,这并不令人惊讶:在布局约束中使用boundingRect(with:options:attributes:context:)
中的估计值并期望它稍后与自动布局计算匹配可能会失败,即使示例不如此精确。这是由于排版的细节,以及可能在不同比例下呈现字体的细节。
您可能想问,您可以避免在此文本视图上设置高度限制吗?由于您希望显示整个文本,因此请务必将UITextView
属性scrollEnabled
设置为false
。这将使其在适当的时间(当设置水平边界时)向Auto Layout引擎提供正确的文本大小。如果它在一个单元格中,使用自动单元大小调整,那么它应该正确调整自身和单元格的大小。希望这可以帮助。
答案 1 :(得分:0)
我弄清楚为什么会出现这种错误。 因为我使用这两行代码自动调整表格视图
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100.0
在后台,UITableViewAutomaticDimension
将自动计算我的单元格高度,并将该数字舍入到小数点后1位
例如:
如果我的单元格总高度为:324.833
它会将该数字舍入324.8
那样。单元格的总高度(324.833
)大于单元格的高度(324.8
)
为了解决这个错误,我只是在UITableViewAutomaticDimension
围绕它之前对我的号码进行了整理