代码之家  ›  专栏  ›  技术社区  ›  Duck

HStack的垂直对齐不起作用,也不直观

  •  0
  • Duck  · 技术社区  · 4 年前

    我有这个代码:

    HStack (alignment:.center) {
      Text("50")
      ProgressView("", value: 50, total: 100)
      Text("100")
      Text("mg")
    }
    

    结果是

    enter image description here

    我想要这个

    enter image description here

    我尝试了所有方法,比如在HStack中添加对齐中心,在VStack中增加进度视图等

    为什么苹果从来没有设法让与界面设计相关的东西直观地工作?

    0 回复  |  直到 4 年前
        1
  •  1
  •   swiftPunk    4 年前

    还有另一种方法,比如不使用“”,它会按照你的意愿工作。

    import SwiftUI
    
    struct ContentView: View {
        var body: some View {
    
            HStack  {
                Text("50")
                ProgressView(value: 50, total: 100)  // <<: Here
                Text("100")
                Text("mg")
            }
    
        }
    }
    

    enter image description here


    你使用的版本也很好,但还有另一种用法,比如代码。它需要 进度视图 实际上,在代码中按下了Text 进度视图 认为 "" 是要显示的信息!这就是为什么你会看到这一点。

    import SwiftUI
    
    struct ContentView: View {
        var body: some View {
    
            HStack  {
                Text("50")
                ProgressView("This part is for information to help", value: 50, total: 100)
                Text("100")
                Text("mg")
            }
    
        }
    }
    

    enter image description here