Reputation: 1586
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
Reputation: 1783
UILabel
.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
Upvotes: 1
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
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:
0
.0.25
. (Note: You can also use 1:4
or 1/4
if those seem more intuitive).Upvotes: 2
Reputation: 26896
You can use a accessory clear-color
view to help get that:
Firstly, create the superview
(blue view), and set the constraints
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
2) Set the assist view
's background color to be clear
The result is this:
Upvotes: 0