Katz
Katz

Reputation: 866

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?

enter image description here

Constrains

enter image description here

Upvotes: 10

Views: 17569

Answers (4)

Kishore Kumar
Kishore Kumar

Reputation: 4375

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.

Upvotes: 0

Mehul Sojitra
Mehul Sojitra

Reputation: 1181

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.

enter image description here

And set horizontal center constraints.

enter image description here

Upvotes: 22

Shubham bairagi
Shubham bairagi

Reputation: 953

just use this code :

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

Upvotes: 0

S. Karthik
S. Karthik

Reputation: 618

enter image description here

  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.

Upvotes: 0

Related Questions