简体   繁体   中英

How to center a label horizontally for all iOS devices in Swift

I can't figure out how to make the label horizontally center in a image view, The label says "You've been here. What would you rate us , I want the What would you rate us to be under the "You've been here." What I attempted to do accomplish this was to add 2 lines to the labels and select the label plus the 3 buttons and clicked on "Add Missing Constrains". When I run the program this is the result.

How can I fix it so the text is entered on all devices?

在此处输入图片说明

Constrains

在此处输入图片说明

for your label try to add constraints like:

Add top, width and height constraints as shown in below screen shot and make sure that constraints to margins is uncheck.

在此处输入图片说明

And set horizontal center constraints.

在此处输入图片说明

Click the label

  1. Don't set any heights

  2. Now set top space to the container view

  3. Centre horizontal to container view

  4. Now set the height (or) bottom space to the smiley

Add this all constraints you will get what you expected.

在此处输入图片说明

  1. Add Vertical top spacing for the particular imageview
  2. Add center horizontal at in same imageview
  3. Then adjust the leading and trailing as same as drag and drop way. it just easy.

just use this code :

yourLabel.translatesAutoresizingMaskIntoConstraints = YES;
CGPoint centerPoint = yourLabel.center;
centerPoint.x = yourImageView.center.x;
yourLabel.center = centerPoint;

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