... because I have an image view that's centered horizontally to its parent, and it's appearing right aligned.
-
1I believe you are not supposed to do that. You should suffice with settings uiStackView's `distribution` and `alignment` properties. If it doesn't work, you are not doing something correctly. You will have to provide a code example to get help with that. – Milan Nosáľ Sep 07 '17 at 20:18
-
1hmmm.... you may need to add width & height constraints. If you show a screen-cap of what you're trying to do, and what's not working the way you expect, it'll be easier to be helpful. – DonMag Sep 07 '17 at 20:25
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.

- 375,296
- 67
- 796
- 848
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.

- 128,072
- 22
- 173
- 272
-
1Granted my answer wasn't as thorough as Rob's, but why the down-vote? – Duncan C Sep 08 '17 at 18:15