... because I have an image view that's centered horizontally to its parent, and it's appearing right aligned.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With