SwiftUI 不同子项对齐和 GeometryReader

标签 swift xcode alignment swiftui geometryreader

我想将 Web 应用程序转换为 iOS 应用程序,但我正在为对齐和 GeometryReader() 苦苦挣扎。

这是我原来的网络应用程序模板的截图:
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 中居中的点图像(如边距:0 自动;在 CSS 中)
  • 外部 HStack 为 100% 宽度,左右边距为 10px(如宽度:100%;边距:自动 10px;在 CSS 中)

  • 我想实现的特殊挑战是,给第二个 Bar() View 一个百分比(或一些等价物),使其具有 1.00/35.69*100% 的百分比。

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

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

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


    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)
    

    它接近我所寻找的:

    enter image description here

    我不明白这里发生了什么。我的目的是让 Bar View 与父 View 具有大小关系。但是父级本身获得了不同的宽度和高度,我需要使用高度为 3 的框架来修复它,并且父级 HStack 会填满整个屏幕。

    当我尝试这个时:
    GeometryReader { g in
        Bar(backgroundColor: Color.gray, width: g.size.width)
        Bar(backgroundColor: Color.black, width: g.size.width*(1/35.69))
    }
    

    我完全出局了,因为条形以某种方式堆叠起来:

    enter image description here

    最佳答案

    我认为这就是你想要的:

    Example

    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)
        }
    }
    

    关于SwiftUI 不同子项对齐和 GeometryReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61819507/

    相关文章:

    ios - 控制台上未显示 Firebase 崩溃报告 - 初学者

    ios - 如何让今天的日期小于 18 年

    swift - 在 cocoa touch 框架中使用 firebase 框架

    css - 带有图片库的 div 不会对齐中心

    BLAST比对算法的Python实现?

    swift - Swift 中的应用程序购买日期、开始或结束日期

    swift - 用户在 macOS 照片中开始项目扩展时加载的 View

    swift - Xcode 8.2 中的 xib 文件无法正常工作。这是一个错误吗?

    swift - 在标签栏中设置自定义图标以突出显示/未突出显示

    html - 对齐图像中心,响应式 div 的底部