该问题似乎是AutoLayout中的一个错误,系统生成的左侧水平约束与标签和单元格视图之间相关,与单元的contentView
相反。这可能已经在XCode 5中修复了。
很多人似乎都认为关闭AutoLayout是答案,但是您失去了AutoLayout的所有优点,只是为了修复一个自定义单元的缩进。它实际上用四行代码很容易修复(和两个IBOutlet
s,其中之一可能已经);
为您的标签,并从他们每个人在Interface Builder CTRL拖动到你UITableViewCell
子类的头文件,你的左水平约束创建IBOutlet
,称他们为“label
”和“leftHorizontalConstraint
”(以代码符合以下,或者使用自己的姓名和更改代码
在你UITableViewCell
超越控制awakeFromNib
的子类,并添加以下代码。
// Remove the constraint that you can't delete in IB (XCode 4)
[self removeConstraint:self.leftHorizontalConstraint];
//Create a dictionary of variable name bindings
NSDictionary *labelDict = NSDictionaryOfVariableBindings(_label);
//Create a horizontal constraint for the label, 20 points from the left edge of its container
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[_label]" options:0 metrics:nil views:labelDict];
//Add the constraint to the containerView
[self.contentView addConstraints:constraints];
当然,如果IB首先在标签和containerView之间生成约束,会容易得多。这是我在XCode 5中修复的,但我没有测试过它。您应该至少能够使用XCode 5手动删除约束,因为IB现在允许您删除系统生成的约束。