Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are you supposed to add constraints to subviews of a Stackview?

... because I have an image view that's centered horizontally to its parent, and it's appearing right aligned.

like image 642
Ian Warburton Avatar asked Sep 07 '17 20:09

Ian Warburton


2 Answers

Yes, you are allowed to add constraints to the arranged subviews of a stack view. From the UIStackView class reference:

You can provide additional constraints to specify the stack view’s height, width, or both.

And later:

You can also fine tune an arranged view’s appearance by adding additional constraints to the arranged view. For example, you can use constraints to set a minimum or maximum height or width for the view. Or you can define an aspect ratio for the view. The stack view uses these constraints when laying out its content. For example, in the image view has an aspect ratio constraint that enforces a constant aspect ratio as the image is resized.

Note

Be careful to avoid introducing conflicts when adding constraints to views inside a stack view. As a general rule of thumb, if a view’s size defaults back to its intrinsic content size for a given dimension, you can safely add a constraint for that dimension.

You should generally not try to use constraints to change the position of an arranged subview within the stack view, because that will almost certainly cause conflicts.

You can constrain the position of an arranged subview relative to other views outside of the stack view, if you intend for those constraints to affect the position of the entire stack view and if you're careful to not cause conflicts. This would be a fairly sophisticated use of constraints.

like image 114
rob mayoff Avatar answered Oct 01 '22 17:10

rob mayoff


You can do whatever you want inside the "tiles" (views) you put into the stack view, but you shouldn't add constraints that alter the size or placement of the tiles themselves. The stack view manages the tiles for you.

like image 44
Duncan C Avatar answered Oct 01 '22 18:10

Duncan C