我有一个带有静态单元格的UITableViewController。在第一个单元格中,我有一个uipickerView。(我在其他的牢房里有其他的东西,但我不认为它是相关的。)我试着用两种方式将pickerView的中心放在细胞里。第一,我在四周增加了4个约束。这给了我一个错误,当我在模拟器上运行它时没有正确显示。第二种方法是,我为x和y增加了2个约束,这也给了我一个错误。
以下是错误:
2015-01-18 15:47:49.533 myApp[1476:54966] 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:0x7fcbc24e9cb0 UITableViewCellContentView:0x7fcbc24da620.centerX == UIPickerView:0x7fcbc24eb1e0.centerX>",
"<NSIBPrototypingLayoutConstraint:0x7fcbc254d7e0 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIPickerView:0x7fcbc24eb1e0](LTR) (Names: '|':UITableViewCellContentView:0x7fcbc24da620 )>",
"<NSIBPrototypingLayoutConstraint:0x7fcbc2556c00 'IB auto generated at build time for view with fixed frame' H:[UIPickerView:0x7fcbc24eb1e0(600)]>",
"<NSLayoutConstraint:0x7fcbc25be170 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7fcbc24da620(375)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fcbc24e9cb0 UITableViewCellContentView:0x7fcbc24da620.centerX == UIPickerView:0x7fcbc24eb1e0.centerX>
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.它对Y说了同样的话,但我把它剪掉了,这样就更清楚了。
我是个初学者,你可能知道,所以请不要对我太苛刻。
更新
我去掉了两个约束(x和y),并在左边和右边添加了一个约束。当我运行它时,pickerView就像ipad一样分散开来(可能是600),它给了我以下错误:
<NSLayoutConstraint:0x7ffcf1d76200 UITableViewCellContentView:0x7ffcf1d0a2d0.trailingMargin == UIPickerView:0x7ffcf1d78a60.trailing - 8>",
"<NSLayoutConstraint:0x7ffcf1d529b0 UIPickerView:0x7ffcf1d78a60.leading == UITableViewCellContentView:0x7ffcf1d0a2d0.leadingMargin - 8>",
"<NSIBPrototypingLayoutConstraint:0x7ffcf1c74700 'IB auto generated at build time for view with fixed frame' H:[UIPickerView:0x7ffcf1d78a60(600)]>",
"<NSLayoutConstraint:0x7ffcf1dbf220 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7ffcf1d0a2d0(375)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7ffcf1d76200 UITableViewCellContentView:0x7ffcf1d0a2d0.trailingMargin == UIPickerView:0x7ffcf1d78a60.trailing - 8>发布于 2015-01-19 02:15:41
实际上,阅读错误消息中的约束列表是相当容易的:它只需要一点练习。你有以下几点:
很容易看出,只要考虑一下,只有当单元格宽为600像素时,才能调节这些像素。但事实并非如此--这是列表中的第四个约束告诉您的:
现在你可能会问:我怎样才能摆脱这场冲突?我甚至没有创建一些这些约束!我认为这样做的方法是删除“居中”约束,并将选择器视图的左边和右边约束到其superview。因此,它的宽度将由其superview的宽度决定,而不是与其冲突。
编辑--我试过了,它运行得很好:

这是在模拟器中运行的静态表视图的单个单元格。选择器视图被添加到单元格中,我应用了三个约束:顶部、左侧和右侧。控制台中没有出现问题。
下面是约束和其他信息,如Size检查器中所示:

https://stackoverflow.com/questions/28016913
复制相似问题