为什么当我使用小数时,出现 [LayoutConstraints] 错误。但是当我舍入那个数字时。错误消失了
Why when I use fraction number, I got [LayoutConstraints] error. But when I round that number. The error gone
我使用 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 的高度(Table View cell auto sizing)
错误发生在第一个约束。
//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
因为如果我不四舍五入它,它会引发错误..
但是inspire of 有一个错误。在我看来一切都还不错
这就是调试windows所说的:
[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.
这绝对是一个奇怪的错误,因为这些错误中的数学加起来(字面意思)。
然而,这并不奇怪:在布局约束中使用 boundingRect(with:options:attributes:context:)
中的估计值并期望它稍后与自动布局计算相匹配,即使示例比这更不精确,也会失败。这是由于排版的细节,以及字体如何以不同比例呈现的细节。
你可能想问,能不能不给这个text view设置高度限制?由于您希望显示整个文本,因此请务必将 UITextView
属性 scrollEnabled
设置为 false
。这将导致它在适当的时间(设置水平边界时)为自动布局引擎提供正确的文本大小。如果它在一个单元格中,使用自动调整单元格大小,那么它应该正确调整自身和单元格的大小。希望这可以帮助。
我弄清楚了为什么会出现这个错误。
因为我用了auto sizing Table 这两行代码查看
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100.0
并且在后台,UITableViewAutomaticDimension
会自动计算我的单元格高度并将该数字四舍五入到小数点后一位
例如:
如果我的单元格总高度是:324.833
它会将该数字四舍五入为 324.8
所以。单元格的总高度 (324.833
) 大于单元格的高度 (324.8
)
为了修复这个错误,我在 UITableViewAutomaticDimension
舍入之前先舍入我的数字
我使用 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 的高度(Table View cell auto sizing)
错误发生在第一个约束。
//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
因为如果我不四舍五入它,它会引发错误..
但是inspire of 有一个错误。在我看来一切都还不错
这就是调试windows所说的:
[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.
这绝对是一个奇怪的错误,因为这些错误中的数学加起来(字面意思)。
然而,这并不奇怪:在布局约束中使用 boundingRect(with:options:attributes:context:)
中的估计值并期望它稍后与自动布局计算相匹配,即使示例比这更不精确,也会失败。这是由于排版的细节,以及字体如何以不同比例呈现的细节。
你可能想问,能不能不给这个text view设置高度限制?由于您希望显示整个文本,因此请务必将 UITextView
属性 scrollEnabled
设置为 false
。这将导致它在适当的时间(设置水平边界时)为自动布局引擎提供正确的文本大小。如果它在一个单元格中,使用自动调整单元格大小,那么它应该正确调整自身和单元格的大小。希望这可以帮助。
我弄清楚了为什么会出现这个错误。 因为我用了auto sizing Table 这两行代码查看
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100.0
并且在后台,UITableViewAutomaticDimension
会自动计算我的单元格高度并将该数字四舍五入到小数点后一位
例如:
如果我的单元格总高度是:324.833
它会将该数字四舍五入为 324.8
所以。单元格的总高度 (324.833
) 大于单元格的高度 (324.8
)
为了修复这个错误,我在 UITableViewAutomaticDimension
舍入之前先舍入我的数字