iOS:如何在代码中为表格单元格中的textfield添加约束
我使用Xcode5,iOSSDK7 我在代码中的表格单元格中创建一个iOS:如何在代码中为表格单元格中的textfield添加约束,ios,uitableview,uitextfield,nslayoutconstraint,Ios,Uitableview,Uitextfield,Nslayoutconstraint,我使用Xcode5,iOSSDK7 我在代码中的表格单元格中创建一个UITextField,而不是在情节提要中。我希望文本字段的宽度应始终与单元格的宽度相同,但我不知道如何实现它 我知道我应该使用NSLayoutConstraint,但我找不到教程,我也尝试过,它们都不起作用 有人能帮我吗?非常感谢 代码片段: cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: ce
UITextField
,而不是在情节提要中。我希望文本字段的宽度应始终与单元格的宽度相同,但我不知道如何实现它
我知道我应该使用NSLayoutConstraint
,但我找不到教程,我也尝试过,它们都不起作用
有人能帮我吗?非常感谢
代码片段:
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: cellIdentifier];
UITextField *textField = [[UITextField alloc]initWithFrame: cell.contentView.frame];
[cell addSubview:textField];
下面是自动布局的链接
多亏@Renish Dadhaniya的回答,我才明白了这一点:
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: cellIdentifier];
UITextField *textField = [[UITextField alloc]initWithFrame: cell.contentView.frame];
[cell addSubview:textField];
[textField setTranslatesAutoresizingMaskIntoConstraints:NO];
NSLayoutConstraint *textLeftConstraint = [NSLayoutConstraint constraintWithItem: textField attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.f];
NSLayoutConstraint *textRightConstraint = [NSLayoutConstraint constraintWithItem: textField attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.f];
[cell addConstraint:textLeftConstraint];
[cell addConstraint:textRightConstraint];
它工作得很好