OGeek|极客世界-中国程序员成长平台

标题: ios - 您是否应该向 Stackview 的 subview 添加约束? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:39
标题: ios - 您是否应该向 Stackview 的 subview 添加约束?

...因为我有一个与其父级水平居中的 ImageView ,并且它看起来是右对齐的。



Best Answer-推荐答案


是的,您可以向堆栈 View 的排列 subview 添加约束。来自 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.

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

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

关于ios - 您是否应该向 Stackview 的 subview 添加约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46104364/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4