xcode 如何在 Swift 中制作全角 UITextField

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33330295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:06:23  来源:igfitidea点击:

How to make a full-width UITextField in Swift

iosxcodeswift

提问by Eduard

I'm very new to Swift, like one week new and I'd like to come up with a 100% width UITextField. The expected result is that the text field should be 100% of the screen's width.

我对 Swift 很陌生,就像一个星期新人,我想提出一个 100% 宽度的 UITextField。预期结果是文本字段应为屏幕宽度的 100%。

This is what I've done for iPhone 5 displays having a 320px vertically centered TextField.

这就是我为 iPhone 5 显示器所做的工作,该显示器具有 320 像素垂直居中的 TextField。

enter image description here

在此处输入图片说明

That looks as expected, but when I'm going on iPhone 6, this happens: enter image description here

这看起来符合预期,但是当我使用 iPhone 6 时,会发生这种情况: 在此处输入图片说明

How is it possible to make this 100% of the screen's width? As I can only see fixed width inputs in size inspector.

怎么可能使屏幕宽度达到 100%?因为我只能在尺寸检查器中看到固定宽度的输入。

回答by luk2302

You should not specify the width but the leading and trailing space of the textfield to its superview:

您不应指定文本字段的宽度,而是指定其父视图的前导和尾随空间:

  • select the UITextFieldyou want to align
  • click on the Pinicon
  • uncheck the "Constrain to margins"
  • set the leading to 0
  • set the trailing to 0
  • 选择UITextField要对齐的
  • 点击Pin图标
  • 取消选中“约束到边距”
  • 将前导设置为 0
  • 将尾随设置为 0

enter image description here

在此处输入图片说明

which will result in the TextField to span the entire width on every screen:

这将导致 TextField 在每个屏幕上跨越整个宽度:

enter image description here

在此处输入图片说明

Note that you sometimes have to specify the view you want to set the space relative to:

请注意,有时您必须指定要设置相对于以下空间的视图:

enter image description here

在此处输入图片说明

回答by Daniel Krom

You can use auto layout and constraints,

您可以使用自动布局和约束,

if you wish, you can do it manually using code:

如果你愿意,你可以使用代码手动完成:

//full screen size
let screenWidth = UIScreen.mainScreen().bounds.width
textInput.frame.size.width = screenWidth