NSConstraints右对齐UITableViewCell内的文本字段

时间:2015-03-24 00:15:55

标签: ios objective-c uitableview nslayoutconstraint

我正在向UITableViewCell添加一个UITextField,并通过以下约束对齐它:

[cell addConstraint:[NSLayoutConstraint constraintWithItem:tf attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:cell.textLabel attribute:NSLayoutAttributeTrailing multiplier:1 constant:8]];
[cell addConstraint:[NSLayoutConstraint constraintWithItem:tf attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeTop multiplier:1 constant:8]];
[cell addConstraint:[NSLayoutConstraint constraintWithItem:tf attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeBottom multiplier:1 constant:-8]];
[cell addConstraint:[NSLayoutConstraint constraintWithItem:tf attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeTrailing multiplier:1 constant:-16]];

这很好用,直到我旋转视图。 UITextField并没有与单元格紧密相关。我只是缺少约束吗?

1 个答案:

答案 0 :(得分:0)

仅供参考我添加:

cell.contentView.autoresizingMask = UIViewContentModeScaleAspectFit;

到UITableCell并修复它!