SwiftUI:文本字段被切断

Jus*_*tMe 2 swiftui

我的 SwiftUI 应用程序上的 textField 被切断了。但它不会每次都发生。这似乎是随机发生的。

显示文本被截断的应用屏幕截图

这是我正在使用的代码:

var body: some View {
    VStack {
      Spacer()
      // Target row
      HStack {
        Text("Put the bullseye as close as you can to:")
        Text("\(target)")
      }
      Spacer()
      // Slider row
      HStack {
        Text("1")
        Slider(value: $sliderValue, in: 1...100) {_ in
          print(self.sliderValue)
        }
        Text("100")
      }
      Spacer()
      // Hit me button row
      Button(action: {
        print("Button pressed")
        self.alertIsVisible = true
      }) {
        Text(/*@START_MENU_TOKEN@*/"Hit Me!"/*@END_MENU_TOKEN@*/)

      }
      .alert(isPresented: $alertIsVisible) { () -> Alert in
        let roundedValue = Int(sliderValue.rounded())
        let score = pointsForCurrentRound()
        return Alert(title: Text("Hello there!"), message: Text("The slider's value is \(roundedValue)!\n" +
          "You scored \(score) points this round"
          ), dismissButton: .default(Text("Awesome")))
      }
      Spacer()
      // Score and start over button row
      HStack {
        Button(action: /*@START_MENU_TOKEN@*/{}/*@END_MENU_TOKEN@*/) {
          Text("Start Over")
        }
        Spacer()
        Text("Score:")
        Text("999999")
        Spacer()
        Text("Round:")
        Text("999")
        Spacer()
        Button(action: /*@START_MENU_TOKEN@*/{}/*@END_MENU_TOKEN@*/) {
          Text("Info")
        }
      }
      .padding(.bottom, 20)
    }
  }
Run Code Online (Sandbox Code Playgroud)

我试过在文本字段后面和目标之前添加填充。我试过在目标的前缘添加填充。我试过在文本字段上使用 frame 方法来添加最小长度。这些都不起作用。有任何想法吗?

谢谢

E.C*_*oms 13

您可以添加 fixedSize() 来锁定标签。

HStack {
  Text("Put the bullseye as close as you can to:").fixedSize()
  Text("\(target)").fixedSize()
}
Run Code Online (Sandbox Code Playgroud)


小智 6

我刚刚遇到了这种情况!经过几分钟的搜索、试验和错误,我终于弄明白了。文本视图正在尝试调整大小,并且其中一个父视图启用了动画。如果有人遇到同样的问题,将 .animation(nil) 添加到文本中,这可能会解决问题。

VStack {
    Text("\(Int(self.viewModel.ProgressPercentage * 100.0))%")
        .font(.largeTitle)
        .animation(nil)
}
Run Code Online (Sandbox Code Playgroud)

祝你好运!