【问题标题】:How do I prevent Text "bounce" in SwiftUI SegmentedControl如何防止 SwiftUI SegmentedControl 中的文本“弹跳”
【发布时间】:2020-06-10 23:00:30
【问题描述】:

当我点击各个片段时,SwiftUI SegmentedPicker 中的文本出现弹跳问题。

这是超级基本的,所以我不确定有哪些选项可以调整它:

struct ContentView : View {
    @State private var selectorIndex = 0
    @State private var numbers = ["One","Two","Three"]

    var body: some View {
        VStack {
            Picker("Numbers", selection: $selectorIndex) {
                ForEach(0 ..< numbers.count) { index in
                    Text(self.numbers[index]).tag(index)
                }
            }
            .pickerStyle(SegmentedPickerStyle())


            Text("Selected value is: \(numbers[selectorIndex])").padding()
        }
    }
}

【问题讨论】:

  • 这是当前版本的 SwiftUI Picker 的“功能”。有些人编写了自己的选择器作为解决方法。
  • 我认为您可能需要创建自定义选择器

标签: swiftui picker


【解决方案1】:

尝试将此添加到文本中,尤其是对齐方式:

.frame(width: 222, height: 55, alignment: .leading)

编辑:

我正在使用以下代码在真实设备和各种模拟器上测试文本弹跳:

struct ContentView: View {

@State private var selectorIndex = 0
@State private var numbers = ["One","Two","Three"]

var body: some View {
    VStack {
        Picker("Numbers", selection: $selectorIndex) {
            ForEach(0 ..< numbers.count) { index in
                Text(self.numbers[index]).tag(index)
            }
        }
        .pickerStyle(SegmentedPickerStyle())
        Text("Selected value is: \(numbers[selectorIndex])")
            .frame(width: 222, height: 55, alignment: .leading)
    }
}
    }

【讨论】:

  • 它确实减少了“反弹”,尤其是在中间部分,但仍然很明显。
  • 我的设置没有任何“反弹”; Mac 10.15.5 使用 xcode 11.6 beta(和 11.5),ios 13.5 iphone 和 iPad,mac 催化剂。你用的是什么系统?
  • 我已经编辑了答案以显示我正在使用的代码。这样我就看不到任何文本反弹
  • 唉,我想我们指的是不同的文本。您更新的代码修改了“选定的值是:一个”文本。虽然我指的是每个段内的文本。这些是我看到移动的文本项。
  • 那怎么样,相信我会选错文字。在这种情况下,我敢打赌这就是 Apple 希望 Pickers 的行为方式,以显示选择的变化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多