gpt4 book ai didi

ios - 当我将 NSLayoutConstraint 设置为特定大小时出现错误

转载 作者:行者123 更新时间:2023-11-29 02:00:06 26 4
gpt4 key购买 nike

我有 4 个 UIButtons,每个按钮的所有 4 个边上都有 约束,并且有一个宽度约束。 (Xcode 让我在其中 3 个上设置了宽度 constraint,但我做了 4 个,只是为了让编码变得更简单。)

然后,我将所有 4 个宽度的约束 添加到 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];

我没有收到任何错误。但是当我将约束设置为:

[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 给我一个错误,指出它无法满足所有约束。但在模拟器中,一切看起来都应该如此。

来自模拟器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.

我该怎么做才能不出现错误?

更新:

这是约束的图像:

enter image description here

最佳答案

错误日志显示您有 4 个按钮,每个按钮都有固定的大小。每个按钮都被限制为与它们最近的相邻按钮(或 TableView 单元格的内容 View 的边缘)之间的间隙为零。为了满足所有这些约束,单元格的宽度必须是按钮的约束宽度之和。但仅更改一个约束常量后,情况就不是这样了:125 + 0 + 62 + 125 != 375。

由于您以编程方式确保按钮的组合宽度填充单元格的宽度,因此您可以移除将最终按钮的后缘与单元格的后缘联系起来的约束。这样,即使只更改了一个约束,也永远不会发生约束冲突。

此外,您的变量 cellWidth 似乎是一个整数而不是 CGFloat,因此您正在进行整数除法,它总是返回一个整数。 375/6 = 62,但 375.0/6.0 = 62.5。

关于ios - 当我将 NSLayoutConstraint 设置为特定大小时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30469354/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com