您应该向 Stackview 的子视图添加约束吗?

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

...因为我有一个图像视图与其父级水平居中,并且显示为右对齐。

您可以在放入堆栈视图的 "tiles"(视图)中做任何您想做的事情,但您不应添加会改变图块本身的大小或位置的约束。堆栈视图为您管理磁贴。

是的,您可以向堆栈视图的排列子视图添加约束。来自 UIStackView class reference:

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

以后:

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.

您通常不应尝试使用约束来更改堆栈视图中已排列子视图的位置,因为这几乎肯定会导致冲突。

如果您打算让这些约束影响整个堆栈视图的位置并且注意不要引起冲突,则可以限制排列的子视图相对于堆栈视图之外的其他视图的位置。这将是对约束的相当复杂的使用。