gpt4 book ai didi

swift - 操作系统 X : "Unable to simultaneously satisfy constraints" with only a single unsatisfiable constraint

转载 作者:行者123 更新时间:2023-11-30 10:20:37 30 4
gpt4 key购买 nike

我正在编写一个基于网格的 View ,使用以下代码动态添加 NSLayoutConstraints

for (x, column) in enumerate(board) {
for (y, cell) in enumerate(column) {
// MARK: Cell set up
cell.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(cell)

// MARK: Autolayout constraints
// Priorities
cell.setContentHuggingPriority(249, forOrientation: .Horizontal)
cell.setContentHuggingPriority(249, forOrientation: .Vertical)
cell.setContentCompressionResistancePriority(750, forOrientation: .Horizontal)
cell.setContentCompressionResistancePriority(750, forOrientation: .Vertical)
// Horizontal layout
if x == 0 {
container.addConstraint(NSLayoutConstraint(item: cell, attribute: .Leading, relatedBy: .Equal, toItem: container, attribute: .Leading, multiplier: 1, constant: 4))
} else {
container.addConstraint(NSLayoutConstraint(item: cell, attribute: .Leading, relatedBy: .Equal, toItem: board[x - 1][y], attribute: .Trailing, multiplier: 1, constant: 4))
container.addConstraint(NSLayoutConstraint(item: cell, attribute: .Width, relatedBy: .Equal, toItem: board[x - 1][y], attribute: .Width, multiplier: 1, constant: 0))
}
if x == board.count - 1 {
container.addConstraint(NSLayoutConstraint(item: container, attribute: .Trailing, relatedBy: .Equal, toItem: cell, attribute: .Trailing, multiplier: 1, constant: 4))
}
// Vertical layout
if y == 0 {
container.addConstraint(NSLayoutConstraint(item: cell, attribute: .Top, relatedBy: .Equal, toItem: container, attribute: .Top, multiplier: 1, constant: 4))
} else {
container.addConstraint(NSLayoutConstraint(item: cell, attribute: .Top, relatedBy: .Equal, toItem: board[x][y - 1], attribute: .Bottom, multiplier: 1, constant: 4))
container.addConstraint(NSLayoutConstraint(item: cell, attribute: .Height, relatedBy: .Equal, toItem: board[x][y - 1], attribute: .Height, multiplier: 1, constant: 0))
}
if y == board[x].count - 1 {
container.addConstraint(NSLayoutConstraint(item: container, attribute: .Bottom, relatedBy: .Equal, toItem: cell, attribute: .Bottom, multiplier: 1, constant: 4))
}
}
}

但是运行时,出现以下错误结果

2014-11-05 17:12:56.333 ProjectName[21373:1094967] Unable to simultaneously satisfy constraints:
(
"<NSLayoutConstraint:0x610000081c70 V:[NSView:0x610000121d60]-(4)-[NSView:0x610000121d60]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x610000081c70 V:[NSView:0x610000121d60]-(4)-[NSView:0x610000121d60]>

Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, break on objc_exception_throw to catch this in the debugger.

现在,board 是一个 NSView 数组的数组,而 NSView 完全是空的。为了得到上面的错误输出,我将board初始化为[[NSView(), NSView()]],即一列两行。

               x = 0
------------
board[x][0] | NSView() |
board[x][1] | NSView() |
------------

我的动态布局代码适用于具有任意数量列的单行,但当给出多于一行时,它会由于某种原因而中断。自动布局行的代码与列代码完全相同,进行左→上、右→下等调整

有人知道为什么多行会破坏我的代码吗?

我使用的是 Xcode 6.1,部署目标为 10.10,使用 swift 进行编程。

最佳答案

请注意,约束中的两项是相同的 View :[NSView:0x610000121d60]。我认为您将相同的 View 放入板数组中两次。

更新

如果您想创建 View 数组:

let column = (0 ..< height).map { _ in NSView() }

关于swift - 操作系统 X : "Unable to simultaneously satisfy constraints" with only a single unsatisfiable constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26768279/

30 4 0
文章推荐: java - JFreeChart 中的自定义堆积面积图
文章推荐: javascript - 未捕获的类型错误 : Object # has no method 'easeOutQuart'