swift 对齐表单中的TextField

yyhrrdl8  于 2023-06-28  发布在  Swift
关注(0)|答案(2)|浏览(161)

你好,我创建了一个4行的表单,每行包含一个文本和2个文本字段,正如你可以从图片中看到的,它们没有垂直对齐,我找不到对齐所有文本字段的方法,并修复文本字段的大小,其中一些是长的,其中一些是短的。

  1. Section("Time Selection"){
  2. HStack{
  3. Text("OFF")
  4. .foregroundStyle(.blue)
  5. .font(.footnote)
  6. TimeInsertView(localTime: $localChockOFF, localTimeDate: $localChockOFFDate, utcTime: $utcChockOFF, utcTimeDate: $utcChockOFFDate, selectedDate: $flightDate, TextBoxDescription: "Chock OFF")
  7. }
  8. HStack{
  9. Text("Take OFF")
  10. .foregroundStyle(.blue)
  11. .font(.footnote)
  12. TimeInsertView(localTime: $localChockIN, localTimeDate: $localChockINDate, utcTime: $utcChockIN, utcTimeDate: $utcChockINDate, selectedDate: $flightDate, TextBoxDescription: "Chock IN")
  13. .onChange(of: utcChockINDate) {
  14. totalBlock = dm.timeDifference(endDate: utcChockINDate, startDate: utcChockOFFDate)
  15. }
  16. }
  17. HStack{
  18. Text("Landing")
  19. .foregroundStyle(.blue)
  20. .font(.footnote)
  21. TimeInsertView(localTime: $localTakeOFF, localTimeDate: $localTakeOFFDate, utcTime: $utcTakeOFF, utcTimeDate: $utcTakeOFFDate, selectedDate: $flightDate, TextBoxDescription: "Take Off")
  22. }
  23. HStack{
  24. Text("IN")
  25. .foregroundStyle(.blue)
  26. .font(.footnote)
  27. TimeInsertView(localTime: $localLanding, localTimeDate: $localLandingDate, utcTime: $utcLanding, utcTimeDate: $utcLandingDate, selectedDate: $flightDate, TextBoxDescription: "Landing")
  28. .onChange(of: utcLandingDate) {
  29. totalFlightTime = dm.timeDifference(endDate: utcLandingDate, startDate: utcTakeOFFDate)
  30. }
  31. }
  32. HStack{
  33. Text("Block Hours")
  34. Spacer()
  35. Text(totalBlock)
  36. }
  37. HStack{
  38. Text("Flight Time")
  39. Spacer()
  40. Text(totalFlightTime)
  41. }
  42. }
swvgeqrz

swvgeqrz1#

我会使用Grid。“网格”的工作就是对齐事物!
假设TimeInsertView是一个HStack,平均分配两个TextField

  1. Grid(alignment: .leading) {
  2. GridRow {
  3. Text("OFF")
  4. .foregroundStyle(.blue)
  5. .font(.footnote)
  6. TimeInsertView(...)
  7. }
  8. // add a Divider here if you like
  9. Divider()
  10. GridRow {
  11. Text("Take OFF")
  12. .foregroundStyle(.blue)
  13. .font(.footnote)
  14. TimeInsertView(...)
  15. }
  16. // and the rest of the grid rows...
  17. }
  18. HStack{
  19. Text("Block Hours")
  20. Spacer()
  21. Text(totalBlock)
  22. }
  23. // and the rest of the Section here...

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

展开查看全部
xa9qqrwz

xa9qqrwz2#

而不是试图改变输入你应该改变标签到一个给定的宽度和输入将自然填补其余的空间

相关问题