我在每个4边都有4 UIButtons
constraints
,宽constraint
。 (Xcode
让我在其中3个上设置宽度constraint
,但我做了4,只是为了使编码更简单。)
然后我将所有4个宽度constraints
添加到NSArray
。当我将宽度constraint
设置为以下内容时:
[self.constraintArray[0] setConstant:cellWdith / 3];
[self.constraintArray[1] setConstant:cellWdith / 3];
[self.constraintArray[3] setConstant:cellWdith / 3];
[self.constraintArray[2] setConstant:0];
我没有收到任何错误。但是当我将constraints
设置为此时:
[self.constraintArray[1] setConstant:cellWidth / 6];
[self.constraintArray[2] setConstant:cellWidth / 6];
[self.constraintArray[3] setConstant:cellWidth / 3];
[self.constraintArray[0] setConstant:cellWidth / 3];
Xcode
给我一个错误,说它无法满足所有constraints
。但在模拟器中,一切看起来都应该如此。
模拟器Xcode
出错:
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:0x7fcd51552710 H:[UIButton:0x7fcd514f64b0'Income'(62)]>",
"<NSLayoutConstraint:0x7fcd5155b630 H:[UIButton:0x7fcd5155b410'All'(125)]>",
"<NSLayoutConstraint:0x7fcd5155daf0 H:[UIButton:0x7fcd5155d8b0'Expense'(0)]>",
"<NSLayoutConstraint:0x7fcd5155de40 H:[UIButton:0x7fcd5156e5e0'Button'(125)]>",
"<NSLayoutConstraint:0x7fcd5156eb30 H:|-(0)-[UIButton:0x7fcd5156e5e0'Button'] (Names: '|':UITableViewCellContentView:0x7fcd515525f0 )>",
"<NSLayoutConstraint:0x7fcd5156ebd0 H:[UIButton:0x7fcd5156e5e0'Button']-(0)-[UIButton:0x7fcd514f64b0'Income']>",
"<NSLayoutConstraint:0x7fcd5157f620 H:[UIButton:0x7fcd514f64b0'Income']-(0)-[UIButton:0x7fcd5155d8b0'Expense']>",
"<NSLayoutConstraint:0x7fcd5157f710 H:[UIButton:0x7fcd5155b410'All']-(0)-| (Names: '|':UITableViewCellContentView:0x7fcd515525f0 )>",
"<NSLayoutConstraint:0x7fcd5157f7b0 H:[UIButton:0x7fcd5155d8b0'Expense']-(0)-[UIButton:0x7fcd5155b410'All']>",
"<NSLayoutConstraint:0x7fcd515bb460 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7fcd515525f0(375)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fcd5155de40 H:[UIButton:0x7fcd5156e5e0'Button'(125)]>
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.
我该怎么办才能收到错误?
更新
以下是constraints
:
答案 0 :(得分:1)
错误日志显示您有4个按钮,每个按钮都有固定大小。每个按钮被约束为在它们与它们最近的相邻按钮之间具有零间隙(或者在边缘上的那些按钮的表格视图单元的内容视图的边缘)。为了满足所有这些约束,单元格的宽度必须是按钮的约束宽度的总和。但只有在你的一个限制因素之后才会出现这种情况。常数已更改:125 + 0 + 62 + 125!= 375.
因为您正在以编程方式确保您的按钮&#39;组合宽度填充单元格的宽度,您可以删除将最终按钮的后缘与单元格的后缘相关联的约束。这样就不会出现约束冲突,即使只有一个约束被改变了。
此外,您的变量cellWidth似乎是一个整数而不是CGFloat,因此您正在进行整数除法,它总是返回一个整数。 375/6 = 62,但375.0 / 6.0 = 62.5。
答案 1 :(得分:0)
尝试此操作:选择所有按钮并清除约束,并将其重置为两者的建议约束(任意宽度 - 任何高度和常规宽度 - 常规高度尺寸类)。它对我有用。