【问题标题】:SwiftUI place around center objectSwiftUI 围绕中心对象放置
【发布时间】:2021-04-26 08:57:17
【问题描述】:

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

如何在带有对齐指南的 SwiftUI 中做到这一点?尺寸未知,但黄色和蓝色高度匹配,绿色和蓝色宽度匹配。

【问题讨论】:

  • 这是否回答了您的问题stackoverflow.com/a/63165931/12299030
  • 您好 Asperi,感谢您抽出宝贵时间。它没有回答我的问题,因为大矩形不在父视图的中心

标签: swiftui swiftui-layout swiftui-zstack


【解决方案1】:

我在推特上得到了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) { $0.height + 10 }
    }
  }
  .overlay(alignment: .trailing) {
    VStack(alignment: .leading, spacing: 0) {
      Color.clear.frame(height: 0)
      Color.blue
        .alignmentGuide(.leading) { $0.width + 10 }
    }
  }
  .padding(100) // padding given for the example 

【讨论】:

    猜你喜欢
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多