gpt4 book ai didi

Swiftui - 禁用键盘在 TextField 中显示

转载 作者:行者123 更新时间:2023-12-03 08:11:58 37 4
gpt4 key购买 nike

对于 swiftui,有什么可以禁用/阻止键盘在 TextField 中显示吗?

因为我正在设计一个计算器并从设计而不是键盘获取输入

最佳答案

您可以将 UITextFieldUIViewRepresentable 结合使用,这样您就可以阻止键盘显示。

import SwiftUI

struct KeyboardView: View {
@State var text: String = ""
@State var placHolder: String = "Enter username"
var body: some View {
VStack {
Spacer()
MyTextField(currentText: $text, placeHolder: $placHolder)
.padding(.horizontal, 40.0)
Spacer()
}
}
}

struct MyTextField: UIViewRepresentable {
@Binding var currentText: String
@Binding var placeHolder: String

func makeUIView(context: Context) -> UITextField {
let textField = UITextField()
textField.inputView = UIView() // hiding keyboard
textField.inputAccessoryView = UIView() // hiding keyboard toolbar
textField.placeholder = placeHolder
textField.textColor = UIColor.black
textField.font = UIFont.systemFont(ofSize: 22.0)
textField.delegate = context.coordinator
return textField
}

func updateUIView(_ textField: UITextField, context: Context) {
textField.text = currentText
}

func makeCoordinator() -> Coordinator {
Coordinator(text: $currentText)
}

class Coordinator: NSObject, UITextFieldDelegate {
@Binding var text: String
init(text: Binding<String>) {
self._text = text
}
}
}

引用:Prevent Keyboard from appearing when tapping on UITextField

关于Swiftui - 禁用键盘在 TextField 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70584234/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com