当我将它们设置为特定大小时,NSLayoutConstraint 出现错误
Get error with NSLayoutConstraint when I set them to certain size
我有 4 个 UIButtons
,每个的 4 个边上都有 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
的图像:
错误日志显示您有 4 个按钮,每个按钮的大小都是固定的。每个按钮都被限制为与它们最近的相邻按钮之间的间隙为零(或者 table 视图单元格的内容视图的边缘,用于边缘上的按钮)。要满足所有这些约束,单元格的宽度必须是按钮的约束宽度的总和。但是,仅更改了一个约束常量后情况就不同了:125 + 0 + 62 + 125 != 375。
由于您以编程方式确保按钮的组合宽度填充单元格的宽度,因此您可以删除将最终按钮的后缘与单元格的后缘联系起来的约束。这样一来,就永远不会发生约束冲突,即使只更改了一个约束。
此外,您的变量 cellWidth 似乎是一个整数而不是 CGFloat,因此您正在进行整数除法,它总是 returns 一个整数。 375 / 6 = 62,但 375.0 / 6.0 = 62.5。
试试这个:Select 所有按钮并清除约束并使其重置为建议的约束(任何宽度 - 任何高度和常规宽度 - 常规高度尺寸 class)。它对我有用。
我有 4 个 UIButtons
,每个的 4 个边上都有 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
的图像:
错误日志显示您有 4 个按钮,每个按钮的大小都是固定的。每个按钮都被限制为与它们最近的相邻按钮之间的间隙为零(或者 table 视图单元格的内容视图的边缘,用于边缘上的按钮)。要满足所有这些约束,单元格的宽度必须是按钮的约束宽度的总和。但是,仅更改了一个约束常量后情况就不同了:125 + 0 + 62 + 125 != 375。
由于您以编程方式确保按钮的组合宽度填充单元格的宽度,因此您可以删除将最终按钮的后缘与单元格的后缘联系起来的约束。这样一来,就永远不会发生约束冲突,即使只更改了一个约束。
此外,您的变量 cellWidth 似乎是一个整数而不是 CGFloat,因此您正在进行整数除法,它总是 returns 一个整数。 375 / 6 = 62,但 375.0 / 6.0 = 62.5。
试试这个:Select 所有按钮并清除约束并使其重置为建议的约束(任何宽度 - 任何高度和常规宽度 - 常规高度尺寸 class)。它对我有用。