Autolayout will help you position the views and maintain those positions if the size of the superview changes. If the superview isn't going to change, you don't really need to mess with constraints -- you can just set the position of the subview appropra
If you're adding view viewB
to view viewA
:
a) To position viewB
so that it's bottom edge corresponds to the bottom edge of viewA
:
viewB.frame.origin.y = viewA.bounds.size.height - viewB.bounds.size.height;
b) You don't say which corner, but it's just a matter of doing the math. For example, the upper right corner of viewA
is at {viewA.bounds.size.x, 0}
in viewA's coordinate system. If you want to put viewB
there, set it's origin to:
{viewA.bounds.size.x-viewB.bounds.size.x, 0}
If you want to add a margin, you can add that to the computation:
int margin = 10;
{viewA.bounds.size.x-viewB.bounds.size.x-margin, margin}
d) Use NSLayoutConstraint to access the autolayout system's constraints programmatically. There's a nice visual format language, so that for your question (a) you could set the constraint for viewA
to:
V:|-[viewB]-0-|
The V
means that you're working in the vertical direction, |
's represent the edges (top and bottom, thanks to the V
) of the superview (that's viewA
), and the 0
means that the distance between viewB
and the bottom of its superview should be 0.