首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >等高的SwiftUI HStack

等高的SwiftUI HStack
EN

Stack Overflow用户
提问于 2020-02-22 04:05:05
回答 2查看 2.4K关注 0票数 3

我希望Text("111")的高度与包含2222的VStack相同...和333……

代码语言:javascript
复制
struct Test7: View {
  var body: some View
  { HStack (alignment: .top) {
    Text( "111")                     // Shall have equal Height
    .background(Color.red)
    VStack(alignment: .leading){.    // of this VStack
      Text("2222222")
      .background(Color.gray)
      Text("333333333")
      .background(Color.blue)
    }
  }
  .background(Color.yellow)}
}

我试着用GeometryReader,但它不能工作

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-22 05:38:54

下面是使用.alignmentGuide的可能方法

代码语言:javascript
复制
struct Test7: View {
    @State
    private var height: CGFloat = .zero // < calculable height

    var body: some View {
        HStack (alignment: .top) {
            Text( "111")                     
                .frame(minHeight: height)    // in Preview default is visible
                .background(Color.red)

            VStack(alignment: .leading) {    
                Text("2222222")
                    .background(Color.gray)
                Text("333333333")
                    .background(Color.blue)
            }
            .alignmentGuide(.top, computeValue: { d in
                DispatchQueue.main.async { // << dynamically detected - needs to be async !!
                    self.height = max(d.height, self.height)
                }
                return d[.top]
            })
        }
        .background(Color.yellow)
    }
}

注意:实际结果仅在LivePreview中可见,因为高度是动态计算的,并在下一个渲染周期中分配,以避免在@State上发生冲突。

票数 8
EN

Stack Overflow用户

发布于 2020-12-10 15:08:45

使用.frame(maxHeight:.infinity)

代码语言:javascript
复制
var body: some View {
        HStack(alignment: .top) {
            Text("111")
                .frame(maxHeight: .infinity)
                .background(Color.red)
            
            VStack(alignment: .leading) {
                Text("2222222")
                    .frame(maxHeight: .infinity)
                    .background(Color.gray)
                        
                Text("333333333")
                    .frame(maxHeight: .infinity)
                    .background(Color.blue)
            }
        }.background(Color.yellow)
            .frame(height: 50)
    }

结果:demo

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60345538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档