如何在容器 (UIView) 内居中对齐 2 个 UIView,而不将它们放在 UIView 内

How to center align 2 UIViews inside a container (UIView), without placing them inside a UIView

居中对齐(水平和垂直)容器 (UIView) 中的 2 个 UIView,而不是将它们放置在 UIView 中。 我知道可以通过将它们放在 UIView 或 UIStackView 中来完成。

但是我们怎么直接做呢??

---面试问题---

REF : -- Sample output

每个人都把事情搞复杂了...

最简单的解决方案:

  • 为所有视图添加中心水平约束
  • 为 View2 添加顶部约束。视图 1.
  • 给View1添加Center Vertical约束,常数为-(View1的高度+View2的高度+它们之间的间距)/2。

感谢面试官。