【发布时间】:2015-07-25 07:53:09
【问题描述】:
我有一个带有一个原型单元的简单 UICollectionVewController。在单元格中,我只有一个 UILabel。
我选择了尺寸等级和自动布局。我为
设置了约束top (Top Space to cell): = 100
Height >= 17
Trailing space to Cell = 0
Leading Space to Cell = 0
在这个配置中它工作正常。但是我想要单元格的左右边距,所以我将前导和尾随空间更改为 10。
虽然这可行,但我收到以下错误:
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:0x1740907c0 H:[UILabel:0x14de1a380'This is a test to see wha...']-(10)-| (Names: '|':UIView:0x14de1a270 )>",
"<NSLayoutConstraint:0x174090810 H:|-(10)-[UILabel:0x14de1a380'This is a test to see wha...'] (Names: '|':UIView:0x14de1a270 )>",
"<NSAutoresizingMaskLayoutConstraint:0x174090d10 h=--& v=--& H:[UIView:0x14de1a270(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1740907c0 H:[UILabel:0x14de1a380'This is a test to see wha...']-(10)-| (Names: '|':UIView:0x14de1a270 )>
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.
如果我将前导空间设置为 10 并删除训练空间并设置宽度约束,则不会出现错误。
但是,单元格的宽度是可变的,所以我无法设置宽度。
我可以通过创建宽度约束的出口并执行以下操作来解决它:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
CGRect colViewSize = collectionView.bounds;
int width = colViewSize.size.width;
width -= 28; // 10px either size and 8px in middle
width = width/2;
width -=14; // margin of 7 (x 2)
cell.labelconstraintWidth.constant = width;
return cell;
然而,这似乎是一个 hack。
有没有人知道这里发生了什么。
【问题讨论】:
标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout