集合视图单元格的自动布局问题

时间:2015-03-07 18:16:55

标签: ios autolayout

我正在尝试创建一个有点基本的布局,并且拥有经典的“无法同时满足约束”的问题。一个小时后,我无法追踪它。

以下是界面构建器中的布局:

interface builder

请注意,代码中未添加/修改任何约束。一切都发生在IB。

以下是我收到的错误消息:

2015-03-07 19:13:33.044 TwitterStats[4326:209207] 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:0x7f888ad8b700 H:[UIImageView:0x7f888ad8ce40(90)]>",
    "<NSLayoutConstraint:0x7f888ad8f910 H:|-(8)-[UIImageView:0x7f888ad8ce40]   (Names: '|':UIView:0x7f888ad8cd70 )>",
    "<NSLayoutConstraint:0x7f888ad8fb40 UIView:0x7f888ad8cd70.trailingMargin == UILabel:0x7f888ad87790'@username'.trailing + 10>",
    "<NSLayoutConstraint:0x7f888ad8fb90 H:[UIImageView:0x7f888ad8ce40]-(14)-[UILabel:0x7f888ad87790'@username']>",
    "<NSAutoresizingMaskLayoutConstraint:0x7f888af29690 h=--& v=--& H:[UIView:0x7f888ad8cd70(50)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f888ad8b700 H:[UIImageView:0x7f888ad8ce40(90)]>

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.
2015-03-07 19:13:33.045 TwitterStats[4326:209207] 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:0x7f888ad8b210 V:[UIImageView:0x7f888ad8ce40(90)]>",
    "<NSLayoutConstraint:0x7f888ad8f450 V:[UIButton:0x7f888ad8f150'Unfollow'(52)]>",
    "<NSLayoutConstraint:0x7f888ad8f8c0 V:|-(8)-[UIImageView:0x7f888ad8ce40]   (Names: '|':UIView:0x7f888ad8cd70 )>",
    "<NSLayoutConstraint:0x7f888ad8fa00 V:[UIImageView:0x7f888ad8ce40]-(10)-[UITableView:0x7f888b840000]>",
    "<NSLayoutConstraint:0x7f888ad8fc30 V:[UITableView:0x7f888b840000]-(0)-[UIButton:0x7f888ad8f150'Unfollow']>",
    "<NSLayoutConstraint:0x7f888ad8fcd0 V:[UIButton:0x7f888ad8f150'Unfollow']-(0)-|   (Names: '|':UIView:0x7f888ad8cd70 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x7f888af3cb10 h=--& v=--& V:[UIView:0x7f888ad8cd70(50)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f888ad8b210 V:[UIImageView:0x7f888ad8ce40(90)]>

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.
2015-03-07 19:13:33.073 TwitterStats[4326:209207] 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:0x7f888ad8f450 V:[UIButton:0x7f888ad8f150'Unfollow'(52)]>",
    "<NSLayoutConstraint:0x7f888ad8f8c0 V:|-(8)-[UIImageView:0x7f888ad8ce40]   (Names: '|':UIView:0x7f888ad8cd70 )>",
    "<NSLayoutConstraint:0x7f888ad8fa00 V:[UIImageView:0x7f888ad8ce40]-(10)-[UITableView:0x7f888b840000]>",
    "<NSLayoutConstraint:0x7f888ad8fc30 V:[UITableView:0x7f888b840000]-(0)-[UIButton:0x7f888ad8f150'Unfollow']>",
    "<NSLayoutConstraint:0x7f888ad8fcd0 V:[UIButton:0x7f888ad8f150'Unfollow']-(0)-|   (Names: '|':UIView:0x7f888ad8cd70 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x7f888af3cb10 h=--& v=--& V:[UIView:0x7f888ad8cd70(50)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f888ad8f450 V:[UIButton:0x7f888ad8f150'Unfollow'(52)]>

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.
2015-03-07 19:13:33.111 TwitterStats[4326:209207] 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:0x7f888ac441e0 H:[UIImageView:0x7f888ac87610(90)]>",
    "<NSLayoutConstraint:0x7f888ac4f8e0 H:|-(8)-[UIImageView:0x7f888ac87610]   (Names: '|':UIView:0x7f888ac7f2c0 )>",
    "<NSLayoutConstraint:0x7f888ac8cd20 UIView:0x7f888ac7f2c0.trailingMargin == UILabel:0x7f888ac52b00'@username'.trailing + 10>",
    "<NSLayoutConstraint:0x7f888ac48b90 H:[UIImageView:0x7f888ac87610]-(14)-[UILabel:0x7f888ac52b00'@username']>",
    "<NSAutoresizingMaskLayoutConstraint:0x7f888af3e030 h=--& v=--& H:[UIView:0x7f888ac7f2c0(50)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f888ac441e0 H:[UIImageView:0x7f888ac87610(90)]>

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.
2015-03-07 19:13:33.113 TwitterStats[4326:209207] 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:0x7f888ac29530 V:[UIImageView:0x7f888ac87610(90)]>",
    "<NSLayoutConstraint:0x7f888ac41d40 V:[UIButton:0x7f888ac53e90'Unfollow'(52)]>",
    "<NSLayoutConstraint:0x7f888ac4f890 V:|-(8)-[UIImageView:0x7f888ac87610]   (Names: '|':UIView:0x7f888ac7f2c0 )>",
    "<NSLayoutConstraint:0x7f888ac8b790 V:[UIImageView:0x7f888ac87610]-(10)-[UITableView:0x7f888b035400]>",
    "<NSLayoutConstraint:0x7f888ac48c30 V:[UITableView:0x7f888b035400]-(0)-[UIButton:0x7f888ac53e90'Unfollow']>",
    "<NSLayoutConstraint:0x7f888ac8ae40 V:[UIButton:0x7f888ac53e90'Unfollow']-(0)-|   (Names: '|':UIView:0x7f888ac7f2c0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x7f888af2ba00 h=--& v=--& V:[UIView:0x7f888ac7f2c0(50)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f888ac29530 V:[UIImageView:0x7f888ac87610(90)]>

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.
2015-03-07 19:13:33.114 TwitterStats[4326:209207] 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:0x7f888ac41d40 V:[UIButton:0x7f888ac53e90'Unfollow'(52)]>",
    "<NSLayoutConstraint:0x7f888ac4f890 V:|-(8)-[UIImageView:0x7f888ac87610]   (Names: '|':UIView:0x7f888ac7f2c0 )>",
    "<NSLayoutConstraint:0x7f888ac8b790 V:[UIImageView:0x7f888ac87610]-(10)-[UITableView:0x7f888b035400]>",
    "<NSLayoutConstraint:0x7f888ac48c30 V:[UITableView:0x7f888b035400]-(0)-[UIButton:0x7f888ac53e90'Unfollow']>",
    "<NSLayoutConstraint:0x7f888ac8ae40 V:[UIButton:0x7f888ac53e90'Unfollow']-(0)-|   (Names: '|':UIView:0x7f888ac7f2c0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x7f888af2ba00 h=--& v=--& V:[UIView:0x7f888ac7f2c0(50)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f888ac41d40 V:[UIButton:0x7f888ac53e90'Unfollow'(52)]>

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.

奇怪的是这个约束<NSAutoresizingMaskLayoutConstraint:0x7f888af2ba00 h=--& v=--& V:[UIView:0x7f888ac7f2c0(50)]>,我不知道它来自哪里。

关于如何解决这些问题的任何想法?

2 个答案:

答案 0 :(得分:0)

昨天我遇到了同样的问题。您提到的NSAutoresizingMaskLayoutConstraint是问题。我无法弄清楚约束的来源,但对于集合视图单元来说似乎是正常的。

问题在于无法满足约束。看看第一组不起作用的约束:

"<NSLayoutConstraint:0x7f888ad8b700 H:[UIImageView:0x7f888ad8ce40(90)]"
"<NSLayoutConstraint:0x7f888ad8f910 H:|-(8)-[UIImageView:0x7f888ad8ce40]   (Names: '|':UIView:0x7f888ad8cd70 )>"
"<NSLayoutConstraint:0x7f888ad8fb40 UIView:0x7f888ad8cd70.trailingMargin == UILabel:0x7f888ad87790'@username'.trailing + 10>"
"<NSLayoutConstraint:0x7f888ad8fb90 H:[UIImageView:0x7f888ad8ce40]-(14)-[UILabel:0x7f888ad87790'@username']>"

从左边缘到图像视图有8个点。 图像视图宽90点。从图像视图到用户名有14个点。然后用户名约束到10个空格的尾随边距。

用户名是UILabel,具有内在大小。假设对于给定用户,它需要40个点来显示用户名。您的约束会导致屏幕宽度为8 + 90 + 14 + 40 + 10 = 162.这将是一个非常窄的屏幕!这些限制无法满足。

这里要做的就是删除这个尾随约束:

"<NSLayoutConstraint:0x7f888ad8fb40 UIView:0x7f888ad8cd70.trailingMargin == UILabel:0x7f888ad87790'@username'.trailing + 10>"

如果您真的担心用户名可能太长,您可以尝试保留此约束,但将其从EQUALS 10更改为GREATER THAN OR EQUAL到10.

答案 1 :(得分:0)

我在故事板中遇到类似UICollectionView的问题。

此错误的原因是CollectionViewCell的contentView的大小。它使用默认大小(50.0,50.0)。这可能是一个InterfaceBuilder-Bug。

修复方法是将imageView.width(90)和按钮宽度(52)的优先级更改为999.