为什么当我使用分数号时,我会出现[layoutconstraints]错误.但是当我绕过这个数字时.错误消失了



i自动通过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

//我的监督纸没有任何限制...我是这样编码的,因为我想通过我的入围高度自动设置超级图的高度(Table View Cell自动尺寸)

错误发生在第一个约束。

//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.

这绝对是一个好奇的错误,因为这些错误中的数学汇总(从字面上看)。

但是,并不奇怪:在布局约束中使用boundingRect(with:options:attributes:context:)中的估计值,并期望以后与自动布局计算匹配,即使示例的精确性不如此,则可能会失败。这是由于排版的详细信息,以及可能以不同尺度呈现字体呈现的详细信息。

您可能想问,您能否避免在此文本视图上设置高度约束?由于您希望整个文本出现,因此请确保将UITextView属性scrollEnabled设置为false。这将导致其在适当的时间(设置水平界限时)为自动布局引擎提供正确的文本大小。如果在单元格中使用自动细胞尺寸,则应正确大小和单元格。希望这可以帮助。

我弄清楚为什么会发生错误。因为我使用了这两条代码

的自动尺寸表视图
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100.0

,在后台,UITableViewAutomaticDimension将自动计算我的单元高度,然后将该数字圆成1个小数位

例如:

如果我的总细胞高度为: 324.833

将该数字围绕324.8

这样。电池的总高度(324.833)大于单元的高度(324.8

为了解决该错误,我只是在UITableViewAutomaticDimension围绕它围绕它的数字

最新更新