11

How to Align subviews trailing end to superviews center X axis using Autolayout in Storyboard?

I have one subview inside a superview and the requirement is to align subviews trailing end to superviews center on X axis.

I may be missing something in the documents but I know the workaround could be to place a invisible UIView of 1 point width at the center of the superview using the constraint Horizontal center in Container that will help me to align subviews trailing end to superview's center.

I am in search of any better way of doing it.

Thanks

2 Answers 2

29

You can just change the constraint to align the trailing edge to the center - first create a normal horizontal centring constraint, and then edit that constraint to change the value to trailing for the subview.

enter image description here

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

-Can you please elaborate your point with some code sample so that It would be more understandable and thanks for your help
There's no relevant code, but I've added screenshots.
This still works. Great find and thanks for posting. I knew it had to be doable.
Update for iOS 9. This still works. But a tip for newbies: if you select the word 'Edit' you only get the constant, offset and multiplier to change. To get all the selections above, double click the Constraint icon (blue thing on the left).
0

You can do it also programmatically.

addSubview(view) view.trailingAnchor.constraint(equalTo: self.centerXAnchor).isActive = true 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.