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

16,443

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

Share:
16,443
Katz
Author by

Katz

I like to listen to coffee and drink music.

Updated on June 09, 2022

Comments

  • Katz
    Katz almost 2 years

    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

  • Katz
    Katz over 8 years
    Do I select the label and the stack view when doing the constrains? because if I don't the buttons don't appear. Since I have to select both stack view and label does the constrain change?
  • Mehul Sojitra
    Mehul Sojitra over 8 years
    Your stack view contains three images(emojis) which is shown in screen shot correct??
  • Mehul Sojitra
    Mehul Sojitra over 8 years
    You say that "if I don't the buttons don't appear" which button you say?(is this after the label? in screen shot).
  • Katz
    Katz over 8 years
    What I did was Select Stack view and label and added 43 as you answered and I did work like I wanted it work, but I have a lot of Issues on the side. for example it says "Frame for Stack View will be different at run time"
  • Katz
    Katz over 8 years
    I updated the question with issues shown on the side.
  • Mehul Sojitra
    Mehul Sojitra over 8 years
    Hi, sorry but Don't consider value from my screen shot as 43 is top space from super view and in your case may be is 29 as you posted in your screen shot. I capture that screen shot only for displaying you which constraints are required please ignore the value of top, width and height contains.
  • Mehul Sojitra
    Mehul Sojitra over 8 years