該問題似乎是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現在允許您刪除系統生成的約束。