【问题标题】:How to make a rectangle's height the same height of a VStack如何使矩形的高度与 VStack 的高度相同
【发布时间】:2020-06-01 21:07:18
【问题描述】:

我有一个 swift 视图,它由一个带有矩形的 HStack 和一个内部文本的 Vstack 组成。我想让矩形的高度与 Vstack 的高度相同。我已经尝试在 StackOverflow 上查看许多其他问题,但没有找到答案。有人可以帮我做吗?

这是我的代码:

struct TodoView: View {
    @State var todos = ["feed the dog", "take the dog out for a walk", "make coffee"]
    @State var height: CGFloat = 45
    var body: some View {
                HStack{
                    RoundedRectangle(cornerRadius: 2)
                        .frame(width: 1)
                        .foregroundColor(Color("lightGray"))
                        .padding()
                    VStack{

                        Text("Todo")
                            .font(.title)
                        ForEach(todos, id: \.self){ todo in
                            Text(todo)
                        }
                    }

                    Spacer()
        }
    }
}

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    您需要知道 GeometryReader 和 PreferenceKey 才能做到这一点。

    struct SiblingHeightKey: PreferenceKey {
        static var defaultValue: CGSize? {
            nil
        }
    
        static func reduce(value: inout CGSize?, nextValue: () -> CGSize?) {
            value = value ?? nextValue()
        }
    }
    
    struct TodoView: View {
        @State var vStackSize: CGSize? = nil
        @State var todos = ["feed the dog", "take the dog out for a walk", "make coffee"]
        @State var height: CGFloat = 45
        var body: some View {
                    HStack{
                        RoundedRectangle(cornerRadius: 2)
                            .foregroundColor(.gray)
                            .frame(width: self.vStackSize?.width, height: self.vStackSize?.height)
                        VStack{
    
                            Text("Todo")
                                .font(.title)
                            ForEach(todos, id: \.self){ todo in
                                Text(todo)
                            }
                        }.background(
                            GeometryReader { proxy in
                                Color.clear.preference(key: SiblingHeightKey.self, value: proxy.size)
                            }
                        )
    
                        Spacer()
                    }.onPreferenceChange(SiblingHeightKey.self) {
                        self.vStackSize = $0
            }
        }
    }
    

    【讨论】:

    • SiblingHeightKey 指的是什么?
    • 已更新答案
    • 我在“Color.clear.preference(key: SiblingHeightKey.self, value: proxy.size) }”行上收到一条错误消息,显示“线程 1:signal SIGBART”
    • 你的错误一定在其他地方,试试这段代码。
    【解决方案2】:

    你可以使用.frame修饰符:

    HStack{
        RoundedRectangle(cornerRadius: 2)
            .frame(width: 1, height: 50)
            .foregroundColor(Color("lightGray"))
            .padding()
        VStack {
            Text("Todo")
                .font(.title)
            ForEach(todos, id: \.self){ todo in
                Text(todo)
            }
            .frame(height: 50)
        }
        Spacer()
    }
    

    如果你想让它们填满整个View

    .frame(minWidth: 0, maxWidth: .infinity)
    

    或者,您可以使用此处建议的GeometryReaderMake a grid of buttons of same width and height in SwiftUI

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多