問題は AutoLayout のバグのようです。システムによって生成された左の水平方向の制約は、セルのcontentView
. これは、XCode 5 で修正されている可能性があります。
AutoLayout をオフにすることが解決策だと多くの人が言っているようですが、そうすると、1 つのカスタム セルのインデントを修正するだけで、AutoLayout のすべての利点が失われます。これは、実際には 4 行のコード (および 2 つIBOutlet
の で、そのうちの 1 つは既にお持ちかもしれません) で非常に簡単に修正できます。
IBOutlet
ラベルと左の水平制約の両方に対して、Interface Builder のそれぞれからサブクラスのヘッダー ファイルに CTRL キーを押しながらドラッグし、 " " と " " をUITableViewCell
呼び出して作成します (以下のコードに一致させるか、独自の名前と名前を使用します)。コードを変更します。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 で修正される可能性がありますが、テストしていません。IB ではシステム生成の制約を削除できるようになったため、少なくとも XCode 5 を使用して手動で制約を削除できるはずです。