【问题标题】:SwiftUI: Text Field getting cut offSwiftUI:文本字段被切断
【发布时间】:2019-11-05 14:03:22
【问题描述】:

我的 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)
    }
  }

我尝试在文本字段的尾随和目标之前添加填充。我尝试在目标的前沿添加填充。我尝试在文本字段上使用 frame 方法来添加最小长度。这些都不起作用。有什么想法吗?

谢谢

【问题讨论】:

  • 您使用的是哪个 Xcode 版本?我使用的是 Xcode 11.1 官方,并且在同一个项目上工作过,它在 iOS 13.1 及更高版本中已修复。
  • 感谢您的回复。我正在使用最新版本的 Xcode 11.2 版。 iOS 13.1 及更高版本中修复了什么?
  • 啊,所以在 iOS 13.1 及更高版本中,Text 根据其大小占用整个空间,而不是截断。你能告诉我它是什么时候随机发生的,在预览版还是模拟器/真实设备中?
  • 它发生在预览版和模拟器中。我还没有在真机上试过。
  • 我刚刚重新安装了 11.1 并试用了该应用程序。文本不再中断。所以一定是11.2中的东西。我会提交错误报告。谢谢你帮我解决这个问题。

标签: swiftui


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 2020-06-01
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多