How to Center a Label Horizontally for All iOS Devices in Swift

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

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.

Sample Image

And set horizontal center constraints.

Sample Image

How to horizontally center aligning a label to fit all screen size in swift

first of all remove all the constraints from the label and put the label where you want to display it after that click on the label into storyBoard and click on Resolve Auto LayOut Issues at the right hand side below and after that click on Add Missing Constraints like shown below.

Sample Image

This works for me correctly.

Place a label in the middle of the screen for all devices Xcode

Right-click-drag from the label onto its container (the background) and select "Center vertically in Container". Repeat for "Center horizontally in Container". That should do it.

How to center UILabel in Swift?

To center a UILabel just add this row

x and y:

title.center = self.view.center

x:

title.center.x = self.view.center.x

y:

title.center.y = self.view.center.y

I am having trouble centering a label in Xcode 8.1.

First thing first: "Center Horizontally in Container" is a constraint.

My initial guess is that you’ve neglected some sort of ambiguity or possibly created a constraint which conflicts with (and overrides) the centering constraint. If this is the case, you should see warnings telling you what the problem is.

As long as there's no vertical ambiguity, and no conflicting horizontal constraints (leading/trailing space), your label should be centered—no Swift code necessary.

I get the impression that you’re fairly new to Xcode, so if you don’t know already, you can check the existing constraints on an object in the Size Inspector, denoted by the ruler in the right sidebar. It’s quite possible that there’s an old constraint you forgot to remove, or one you added but don’t need.

Another thing to note: I’ve experienced some layout issues when switching devices in the new Interface Builder in Xcode 8—it’s just a bit buggy in my experience, especially with regard to Stack Views. If you have doubts about how something looks, I’d recommend running the app in Simulator for some devices you don’t have on hand to check it out.

How to fix constraints for 4 horizontal label?


Your layout looks like it could benefit from being in a tableView, but let's look at just your horizontal constraints.

This is a great place to use a Stack View. Do the following:

  1. Get rid of all horizontal constraints for the 4 labels.
  2. Select the 4 labels (by holding shift and clicking on each label) and then choose Editor -> Embed In -> Stack View from the Xcode menu.
  3. In the Attributes Inspector on the right, make sure Stack View Axis is Horizontal.
  4. Set Alignment to Fill, Distribution to Fill Equally, Spacing to 16.
  5. Give your Stack View leading and trailing constraints to Safe Area leading and trailing.

and you're done. Your labels will grow equally to fill in the space for each phone.


Setup in Xcode


Next steps:

You can compose Stack Views (put Stack Views inside other Stack Views).

For your setup:

  1. Add a label above the 4 labels for the day of the week (eg. Lundi).
  2. Select this new label and the 4-labels Stack View and embed those into a Vertical Stack View, Alignment: Fill, Distribution: Fill Equally, Spacing: 16.
  3. Make six more copies of this new Stack View for the other days of the week, and embed those in a Vertical Stack View, Alignment: Fill, Distribution: Equal Spacing.
  4. For this outermost Stack View, give it constraints to place it on the screen. If you constrain its top, bottom, leading, and trailing edges to the Safe Area, the Stack View will fill the phone's screen, and the days of the week will be spaced out to fill it nicely.

Center multiple independent items in swift (storyboard)

Yes you can add constraints to each of the item as i have added , i have added the image for each view and the constraints applied for each of them

Constraints explained:-

1) For the center label , i have added top constraint and centered it horizontally

2) For all the imageView i have pinned the width and height of the images you can do that as per you're requirement

3) I have aligned the top of each of the image to the labels and the labels top to the next image - so that they remain in the same horizontal line

4) Added horizontal spacing between the image and the label and also between the label and the next image so that they remain together

5) Added center horizontal constraint to the "by author" label since it is in the center and since all the other labels and images have horizontal spaces linked with each other the entire unit will stay center aligned.

6) Added a vertical spacing between the second image and the "center label"

Sample Image

Sample Image

Sample Image

Sample Image

Sample Image

Sample Image

Sample Image

And if you run it on any device (iPhone/ipad) it would be in center as per your requirement , i have added 2 images to show the output

Sample Image

Sample Image

How to set top-left alignment for UILabel for iOS application?

Rather than re-explaining, I will link to this rather extensive & highly rated question/answer:

Vertically align text to top within a UILabel

The short answer is no, Apple didn't make this easy, but it is possible by changing the frame size.

Horizontal UIStackView - how to align items inside so that one is floated left and the other sticks in center?

You have a few options...


The simplest, and closest to your CSS description:

Sample Image

Button is constrained 0 from top, 8 from leading (adjust as desired); label is constrained centered vertically to button, centered horizontally to view.

The drawback: if label gets too much text, it will overlap the button.


Second option:

Sample Image

Same as above, but add a "spacer" UIView to upper right... constrain it 0 to top, 8 to trailing, centered vertically to button, and constrain it equal width and equal height to button. Then, add >= 8 constraints to the label leading to button trailing, and label trailing to spacer view leading. That will allow the label to expand horizontally based on its text, but will prevent overlap (text will be ... truncated if there is too much to fit).


Third option - using stack view:

Sample Image

Stack view settings:

Axis: Horizontal
Alignment: Fill
Distribution: Fill
Spacing: 8

Constrain the stack view 0 to top, 8 leading and trailing (just for a little padding on the sides), add a button, label and UIView. Constrain the
"spacer" view equal width to the button. Set the Horizontal Content Hugging Priority = 1000 for the button (will keep it the width of its title).

There are other approaches, partially depending on if you want to do anything else you haven't shown here, but all three of these will do the job.



Related Topics



Leave a reply



Submit