简体   繁体   English

UITableViewCell 中 UITextField 的宽度

[英]Width of UITextField in UITableViewCell

I have a UITextfield in a UITableViewCell.我在 UITableViewCell 中有一个 UITextfield。 I want the width of the UITextfield to be equal to the width of UITableViewCell.我希望 UITextfield 的宽度等于 UITableViewCell 的宽度。 Refer to screenshot below.请参阅下面的屏幕截图。

截图:我想要的文本字段宽度

I have tried the following but it did not work:我已经尝试了以下但没有奏效:

  • Add Auto Layout constraints: leading, trailing, top and bottom.添加自动布局约束:前导、尾随、顶部和底部。
  • Ctrl-Drag UITextfield to ContentView and selected 'Equal Width'.按住 Ctrl 键并拖动 UITextfield 到 ContentView 并选择“等宽”。

Applying any constraints causes the width to shrink, as seen in the screenshot below.应用任何约束都会导致宽度缩小,如下面的屏幕截图所示。 屏幕截图:应用自动布局约束导致宽度缩小

Swift 3 codes in custom table cell自定义表格单元格中的 Swift 3 代码

func configure(text: String, placeholder: String) {
    emailTextField.text = text
    emailTextField.placeholder = placeholder
    emailTextField.accessibilityValue = text
    emailTextField.accessibilityLabel = placeholder
    emailTextField.autocorrectionType = .no
}

Any advice will be much appreciated.任何建议将不胜感激。

Attempt 1尝试 1

  • Remove existing textfield删除现有的文本字段
  • Add a new texfield添加一个新的文本域
  • Apply leading, top, trailing and bottom with 8 points.应用前导、顶部、尾随和底部 8 个点。
  • Build and run the app in Simulator shows that the width still doesn't expand to the width of UITableViewCell.在模拟器中构建并运行应用程序显示宽度仍然没有扩展到 UITableViewCell 的宽度。

尝试 1

根据图像显示添加约束,它工作正常。

Add Auto Layout Constrains:Leading,Top and Width of the text field should equal to superView width and adjust the Multiplier according to it.添加 Auto Layout Constrains:Leading、Top 和 Width 文本字段应等于 superView 宽度并根据它调整 Multiplier。

Hope it works, sorry if I'm wrong.希望它有效,如果我错了,抱歉。

I have added the two different simulator screenshots for yours reference.我添加了两个不同的模拟器屏幕截图供您参考。

iPhone 7 Simulator Screenshot iPhone 7 模拟器截图

iPhone SE simulator Screenshot iPhone SE 模拟器截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM