简体   繁体   中英

How to center TextField using Xcode constraints?

I am new to iOS and am trying to align these TextFields in iOS. Below is how I would like it to look on an iPhone 7.

居中图像

However, on the iPhone 7 Plus, the TextField shifts to the left of the center, despite my constraints.

非中心图像 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
  • Select view you have to add constraint
  • Click on align button at the right - bottom corner
  • Set horizontally in container constraint 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. And also easily scrolling if device is display small.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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