你好,我创建了一个有4行的表单,每行包含一个文本和2个文本字段,正如你从图片中看到的那样,它们不是垂直对齐的,我找不到对齐所有它们的方法和固定文本字段的大小,它们中的一些是长的,有些是短的.

pic

       Section("Time Selection"){
                    HStack{
                        Text("OFF")
                            .foregroundStyle(.blue)
                            .font(.footnote)
                            
                        TimeInsertView(localTime: $localChockOFF, localTimeDate: $localChockOFFDate, utcTime: $utcChockOFF, utcTimeDate: $utcChockOFFDate, selectedDate: $flightDate, TextBoxDescription: "Chock OFF")
                    }
                    HStack{
                        Text("Take OFF")
                            .foregroundStyle(.blue)
                            .font(.footnote)
                        TimeInsertView(localTime: $localChockIN, localTimeDate: $localChockINDate, utcTime: $utcChockIN, utcTimeDate: $utcChockINDate, selectedDate: $flightDate, TextBoxDescription: "Chock IN")
                            
                            .onChange(of: utcChockINDate) {
                                totalBlock = dm.timeDifference(endDate: utcChockINDate, startDate: utcChockOFFDate)
                            }
                    }
                    HStack{
                        Text("Landing")
                            .foregroundStyle(.blue)
                            .font(.footnote)
                        
                        TimeInsertView(localTime: $localTakeOFF, localTimeDate: $localTakeOFFDate, utcTime: $utcTakeOFF, utcTimeDate: $utcTakeOFFDate, selectedDate: $flightDate, TextBoxDescription: "Take Off")
                    }
                    HStack{
                        Text("IN")
                            .foregroundStyle(.blue)
                            .font(.footnote)
                        TimeInsertView(localTime: $localLanding, localTimeDate: $localLandingDate, utcTime: $utcLanding, utcTimeDate: $utcLandingDate, selectedDate: $flightDate, TextBoxDescription: "Landing")
                            .onChange(of: utcLandingDate) {
                                totalFlightTime = dm.timeDifference(endDate: utcLandingDate, startDate: utcTakeOFFDate)
                            }
                    }
                    
                    HStack{
                        Text("Block Hours")
                        Spacer()
                        Text(totalBlock)
                    }
                    HStack{
                        Text("Flight Time")
                        Spacer()
                        Text(totalFlightTime)
                    }
                    
                }

推荐答案

我会用Grid美元."网格"的工作就是对齐事物!

假设TimeInsertView是一个HStack,它平均分配两个TextField

Grid(alignment: .leading) {
    GridRow {
        Text("OFF")
            .foregroundStyle(.blue)
            .font(.footnote)
        TimeInsertView(...)
    }
    // add a Divider here if you like
    Divider()
    GridRow {
        Text("Take OFF")
            .foregroundStyle(.blue)
            .font(.footnote)
        TimeInsertView(...)
    }
    // and the rest of the grid rows...
}
HStack{
    Text("Block Hours")
    Spacer()
    Text(totalBlock)
}
// and the rest of the Section here...

这不依赖于最左侧列的硬编码值.Grid在左列找到最宽的Text,并相应地调整右列的宽度.

Swift相关问答推荐

为什么Swift在某些链调用中不能对不可变值使用变异成员,而在其他链调用中则不能使用变异成员?

{Singleton类}类型的值没有成员$showegView'

@ MainActor + Combine不一致的编译器错误

如何在realityKit中反转USDZ动画

如何使用Swift宏向 struct 体及其init函数添加新成员?

依赖于@Environment的init@StateObject

我在SwiftUI中的动画无法正常工作

background Task(.backgroundTask)不适用于MySQL

异步/等待函数的XCTAssertThrowsError

为什么我不能在这个 Swift 间接枚举中返回 self ?

如何使用Duration.TimeFormatStyle以h m s格式快速格式化时间

MainActor 隔离可变存储属性为何会出现可发送错误?

Swift 非参与者隔离闭包

为什么 Swift Tuples 只能在元素个数小于等于 6 的情况下进行比较?

在 Swift 中强制崩溃的最简单方法

Xcode 7.3 Swift 的语法高亮和代码完成问题

Swift:在子类中覆盖 == 导致仅在超类中调用 ==

Swift 协议只能设置?

Type 应该采用什么协议来让泛型函数将任何数字类型作为 Swift 中的参数?

Switch 语句中的字符串:String不符合协议IntervalType