Beginning Auto Layout · Editing Constraints | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/7478-beginning-auto-layout/lessons/14

Thanks very much for your question!

You can definitely center a UIView between other UIViews using Auto Layout. StackView was later introduced to help make things easier for developers. Prior to StackViews, developers would group views together inside a UIView, and then use that view to position it within the parent view.

With respect to affecting performance, this would have to be an extreme scenario for something like stackViews to affect performance, in which case, I would think that there was something wrong with how you designed your layout. As I said, prior to stackViews, developers were using a similar technique before, so using this approach shouldn’t be a problem.

To specifically answer your question, with respect to centring a UIView between two UIViews, in IB, when you are setting the constraints, you simply have to ensure that the constraints on either side of the view you are centring are identical, thus, ensuring that it is spaced evenly (i.e. centred) between both UiViews.

I hope this helps!

All the best!

At timestamp 1:25, Jessy states “you can change whether the constraint is connected to a view’s margin or its edge.” Previously we were told to set the constraint to 0 from the view. What is the difference? Is there a benefit of one or the other?

@catie @jessycatterwaul @jcatterwaul Can you please help with this when you get a chance? Thank you - much appreciated! :]

1 Like

Hi! One isn’t really better than the other, they’re just different.

A view’s edge is literally the edge of the view. The margins of a view are values that are (usually) inset by some amount from each edge. If you’re doing something like adding an image that you want to cover the entire horizontal distance of a view, you probably want to constrain to the view’s edge. If you wanted to place a bunch of labels inside a parent view, but want their left edges to all start slightly inset from the edge of the view, constrain to the leading margin.

If you’d like to read more about this topic specifically, there’s a great blog post that just came out yesterday about layout margins and the related-but-different safe area.

1 Like

Thank you, @catie and @shogunkaramazov!! The article you referenced @catie is great!

1 Like