简体   繁体   English

如何使用Xcode约束使TextField居中?

[英]How to center TextField using Xcode constraints?

I am new to iOS and am trying to align these TextFields in iOS. 我是iOS的新手,正在尝试在iOS中对齐这些TextField。 Below is how I would like it to look on an iPhone 7. 以下是我希望它在iPhone 7上的外观。

居中图像

However, on the iPhone 7 Plus, the TextField shifts to the left of the center, despite my constraints. 但是,在iPhone 7 Plus上,尽管有我的限制,但TextField却移到了中心的左侧。

非中心图像 Why is this? 为什么是这样? I have a constraint that aligns them to the Center, as seen on the right part of the screenshot. 我有一个约束使它们与中心对齐,如屏幕截图的右侧所示。

You just have to seth trailing and leading space fixed means both left and right indicators should be red as shown in the screenshot. 您只需要固定尾随和前导空格就可以了,这意味着左右指示器都应为红色,如屏幕截图所示。

For more details see the screenshots: 有关更多详细信息,请参见屏幕截图:

在此处输入图片说明

It will remain fixed from left and right in all the screens. 在所有屏幕中,它将保持从左到右的固定状态。

It is very simple!! 这很简单! Just follow below steps, 只需按照以下步骤操作,

  • Open you .xib or .storyboard file 打开您的.xib或.storyboard文件
  • Select view you have to add constraint 选择您必须添加约束的视图
  • Click on align button at the right - bottom corner 单击右下角的“对齐”按钮
  • Set horizontally in container constraint 0 在容器约束中水平设置0
  • Then press add constraints button 然后按添加约束按钮

You can add any constraints to any view by following these steps 您可以按照以下步骤将任何约束添加到任何视图

in your case is important set the width constraint. 在您的情况下,重要的是设置宽度约束。 However you can use trailing and leading constraints, in this way you will not need to set a defined width for your textfield and the position is correct for all type of devices. 但是,您可以使用尾随和前导约束,这样就无需为文本字段设置定义的宽度,并且位置对于所有类型的设备都是正确的。

If you want design like this so you have to take UITableview then take UITextfield for every cell beacuse you use every cell as view so easily manage all textfield and constraint. 如果要进行这样的设计,则必须使用UITableview,然后将UITextfield用于每个单元格,因为您将每个单元格用作视图,因此可以轻松管理所有文本字段和约束。 And also easily scrolling if device is display small. 如果设备显示较小,也可以轻松滚动。

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

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