Scott Kilbourn
Scott Kilbourn

Reputation: 1586

Position UILabel in Storyboard

I am trying to position a UILabel within a UIView on a storyboard, without any code. I'd like the label to have an X position of 1/4 of the width of the UIView. So, if the width of the UIView is 400, I want the UILabel to be at X = 100.

Is it possible to do this on the storyboard with constraints without code?

Upvotes: 2

Views: 2054

Answers (4)

UdayM
UdayM

Reputation: 1783

  1. First give all other constrains to the UILabel.
  2. to set X position as 1/4 of the width of the UIView

    Consider trailing margin of Superview. If trailing margin is 60, then width of the view will be 60

enter image description here

Upvotes: 1

Rikh
Rikh

Reputation: 4222

Apart from the answer below what you can do is add a bottom and trailing of your UILabel to your superview. Now control-drag your UILabel to your superview and select the equal width constraint and set the multiplier to 0.75.

The above will allow your your UILabel to always have a width of 3/4 to your superview and since it will have a trailing of 0 to your superview there will always be a space offset by 1/4 the width of your superview

Upvotes: 0

vacawama
vacawama

Reputation: 154613

You need to create a constraint such that the leading edge of your label is equal to the trailing edge of your superview with a constant of 0 and a multiplier of 0.25.

To create this in the Storyboard:

  1. control-drag from the label to the right edge of its superview.
  2. Choose Trailing Space to Container from the pop-up.
  3. Now edit the constraint. Click on the I-bar that connects your label to the right of its superview. Open the Attributes Inspector. Choose the First Item pop-up and select Reverse First and Second Item.
  4. The First Item should now be the Trailing edge of your label. Click on that and change it to Leading.
  5. Change the constant to 0.
  6. Change the multiplier to 0.25. (Note: You can also use 1:4 or 1/4 if those seem more intuitive).

Constraint in Storyboard

Upvotes: 2

aircraft
aircraft

Reputation: 26896

You can use a accessory clear-color view to help get that:

  1. Firstly, create the superview(blue view), and set the constraints enter image description here

  2. Secondly, you can use a assist view, set it's width is 1/4 of the blue view

1) When you set the assist view's width constraint , you can drag the assist view to the blue view, choose the Equals Width, the in the Size inspector you can edit the width constraint's Multiplier to be 1:4

enter image description here

2) Set the assist view's background color to be clear

enter image description here

The result is this:

enter image description here

  1. The third step, you can drag the label, set the label's left constaint to the assist view enter image description here

Upvotes: 0

Related Questions