SwiftUI不同的子视图对齐方式和GeometryReader

4
我想将网络应用程序转换为iOS应用程序,但我在对齐和GeometryReader()方面遇到了困难。
这是我原始的Web应用程序模板截图: enter image description here 这是我在SwiftUI中的当前版本: enter image description here 相关代码如下:
struct PileRow: View {
    var body: some View {

        HStack() {
            VStack(alignment: .leading, spacing: 3) {
                Text("Adobe".uppercased())
                Bar(backgroundColor: Color.gray, width: 200)
                Bar(backgroundColor: Color.black, width: 200)
                HStack(alignment: .top, spacing: 3) {
                    Text("EUR 1,00")
                        .fontWeight(.light)
                    Text(" / ")
                        .fontWeight(.light)
                        .foregroundColor(Color.gray)
                    Text("35,69")
                        .fontWeight(.light)
                        .foregroundColor(Color.gray)
                }
                Image("dots")
                    .resizable()
                    .scaledToFit()
                    .frame(width: 20)
            }
            .font(.system(size: 14))
            .padding()
        }
        .background(Color.white)
        .cornerRadius(15)
        .shadow(radius: 6, x: 5, y: 5)
    }
}

struct Bar: View {

    var backgroundColor: Color
    var width: CGFloat

    var body: some View {
        Rectangle()
            .fill(backgroundColor)
            .frame(width: width, height: 3)
            .cornerRadius(1.5)
    }
}

我希望:

  • 点图像在父级VStack中居中 (类似于CSS中的 margin: 0 auto;)
  • 外部HStack宽度为100%,具有左右10px的边距 (类似于CSS中的 width: 100%; margin: auto 10px;)

我想要实现的特殊挑战是,给第二个Bar()视图一个百分比值(或等效值),其宽度为1.00 / 35.69 * 100%。

在Swift UI的苹果文档中,我发现:

GeometryReader 一个容器视图,将其自身的大小和坐标空间定义为其内容的函数。3

改变前的代码:

Bar(backgroundColor: Color.gray, width: 200)
Bar(backgroundColor: Color.black, width: 200)

to

GeometryReader { g in
    Bar(backgroundColor: Color.gray, width: g.size.width)
}
.frame(height: 3)
GeometryReader { g in
    Bar(backgroundColor: Color.black, width: g.size.width*(1/35.69))
}
.frame(height: 3)

这接近我所寻求的:

在此输入图片描述

我不理解这里正在发生什么。 我的目的是给Bar视图与父视图的大小关系,但父视图本身得到了不同的宽度和高度,我需要用一个高度为3的框架来修复,父HStack填满整个屏幕。

当我尝试这样做时:

GeometryReader { g in
    Bar(backgroundColor: Color.gray, width: g.size.width)
    Bar(backgroundColor: Color.black, width: g.size.width*(1/35.69))
}

我完全跟不上了,因为这些栏目似乎被垂直叠放起来:

进入图片描述

1个回答

2
我认为这就是你想要的:

示例


import SwiftUI

struct ContentView: View {
    var body: some View {

        GeometryReader { g in
            HStack {
                VStack(alignment: .leading, spacing: 3) {
                    Text("Adobe".uppercased())
                    Bar(backgroundColor: Color.gray, width: g.size.width)
                    Bar(backgroundColor: Color.black, width: g.size.width*(1/35.69))
                    HStack(alignment: .top, spacing: 3) {
                        Text("EUR 1,00")
                            .fontWeight(.light)
                        Text(" / ")
                            .fontWeight(.light)
                            .foregroundColor(Color.gray)
                        Text("35,69")
                            .fontWeight(.light)
                            .foregroundColor(Color.gray)
                    }
                    HStack {
                        Spacer()
                        Image(systemName: "dots")
                            .resizable()
                            .scaledToFit()
                            .frame(width: 20)
                        Spacer()
                    }
                }
                .font(.system(size: 14))
                .padding()
            }
            .background(Color.white)
            .cornerRadius(15)
            .shadow(radius: 6, x: 5, y: 5)
        }
        .padding()
    }
}

struct Bar: View {

    var backgroundColor: Color
    var width: CGFloat

    var body: some View {
        Rectangle()
            .fill(backgroundColor)
            .frame(width: width, height: 3)
            .cornerRadius(1.5)
    }
}

感谢您的回答。这生成了正确的边距(带有GeometryView上的填充),但Image("dots")未在VStack中居中,当我将ContentView()作为List()视图的子项时,项目会重叠。 - T. Karter
1
请查看我的更新答案,现在正确地将“点”居中。对于您的其他问题,我无法在评论中提供帮助,也不知道您使用的代码,但我的答案回答了您在原始问题中提出的所有问题。也许可以单独提出新问题,并将此问题标记为已解决。 - Kuhlemann
答案非常简单 - 我喜欢它!谢谢!一切都解决了。 - T. Karter
没问题!如果这个答案对你有帮助,请给它点赞。 :-) - Kuhlemann

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接