karim
karim

Reputation: 15599

ios storyboard place a view to right of the middle of superview

Search a bit but could not find a solution. Suppose I have a label and a text field. I want the textfield to be aligned 10 pixel to the right of middle of its superview having a width of half of superview - 20 pixel. i.e. 10 px gap in both side. How can I achieve that in storyboard? Is it possible or I have write code for that? Similarly Label is half of superview - 20 px width. Left aligned. 10 px left gap.

Upvotes: 1

Views: 1728

Answers (3)

Jakub Vano
Jakub Vano

Reputation: 3873

If I understand you correctly, you want this with both views having the same width?

|-10-[UILabel]-20-[UITextField]-10-|

Then one way to go about this is to create constraints for

  • leading margin for UILabel
  • horizontal spacing between UILabel and UITextField
  • trailing margin for UITextField
  • same width of UILabel and UITextField

Upvotes: 2

Arthur Gevorkyan
Arthur Gevorkyan

Reputation: 2107

  • So:

I want the textfield to be aligned 10 pixel to the right of middle of its superview That could be done like that: enter image description here Horizontal centering constraint with offset to the right.

  • And now

having a width of half of superview - 20 pixel. i.e. 10 px gap in both side.

To achieve that you should select your parent view and your subview(in your case, the text field) by holding the command key and left-clicking both you view and the text field. See the image below:

enter image description here

Having those two selected, click the Pin button at the bottom of the IB editor area and make all as illustrated below: enter image description here

Then, go to the inspector area of either your parent view or your text field and make it be like (the image is captured from the text field's inspector). And notice: you should select the equal width constraint and type the values manually. When you change the multiplier from 1 to 0.5, the constraint will become of type "proportional width" and that is what you are eager for.

enter image description here

  • Setting right margins and horizontal inter-space should not be a problem. Enjoy coding, man :)

Upvotes: 5

André Slotta
André Slotta

Reputation: 14040

if i got you right this is totally possible in storyboard. setup the following constraints:

  1. pin the labels leading to the leading of its superview with a constant of 10
  2. pin the labels trailing to the leading of the textfield with a constant of 20
  3. pin the textfields trailing to the trailing of its superview with a constant of 10
  4. create an equal widths constraint for the label and the textfield

hope i got you right :)

Upvotes: 1

Related Questions