SwiftUI 围绕中心对象放置

SwiftUI place around center object

假设蓝色矩形在红色矩形的中心,绿色需要在上面,黄色在左边。

如何使用对齐指南在 SwiftUI 中执行此操作? None 个尺寸已知,但黄色和蓝色高度匹配,绿色和蓝色宽度匹配。

我在推特上得到了 https://swiftui-lab.com 的帮助

用我的辅助方法:

extension View {
    func overlay<Overlay: View>(alignment: Alignment, @ViewBuilder builder: () -> Overlay) -> some View {
        overlay(builder(), alignment: alignment)
    }
}

可以这样做:

Color.red
  .overlay(alignment: .bottom) {
    HStack(alignment: .top, spacing: 0) {
      Color.clear.frame(width: 0)
      Color.yellow
      .alignmentGuide(.top) { [=11=].height + 10 }
    }
  }
  .overlay(alignment: .trailing) {
    VStack(alignment: .leading, spacing: 0) {
      Color.clear.frame(height: 0)
      Color.blue
        .alignmentGuide(.leading) { [=11=].width + 10 }
    }
  }
  .padding(100) // padding given for the example