【问题标题】:Swiftui: align selected text in picker to leading edgeSwiftui:将选择器中的选定文本与前沿对齐
【发布时间】:2021-05-25 14:03:40
【问题描述】:

目前我有一个 Section 包含在 Form 中的 picker 我想要达到的是将 picker 的选定值与 iOS 13 和 14 中的前导对齐,我尝试了很多解决方案,例如labelsHidden(),但没有结果,请在iOS 14上找到生成以下屏幕截图的代码示例,任何帮助将不胜感激

struct ContentView: View {
    @State private var selectedStrength = "Mild"
    let strengths = ["Mild", "Medium", "Mature"]

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker("", selection: $selectedStrength) {
                        ForEach(strengths, id: \.self) {
                            Text($0)
                        }
                    }
                }
            }
        }
    }
}

【问题讨论】:

  • 似乎只有在Form 内使用Picker 时才会出现问题...
  • @aheze 实际上我需要去一个单独的屏幕从数组中选择一些值,这就是为什么在这种情况下Form 是必要的

标签: swiftui ios14 swiftui-navigationview swiftui-form


【解决方案1】:

HStack() 中使用Text()Spacer()

struct ContentView: View {
    @State private var selectedStrength = "Mild"
    let strengths = ["Mild", "Medium", "Mature"]

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker("", selection: $selectedStrength) {
                        ForEach(strengths, id: \.self) { t in
                            HStack {
                                Text(t)
                                Spacer()
                            }
                        }
                    }
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2022-01-01
    • 2018-02-09
    • 2012-03-09
    • 2015-06-15
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多