use*_*617 23 keyboard xcode ios swiftui
我正在尝试找到一种向 SwiftUI 数字键盘添加键或按钮的方法。我发现的唯一参考资料说这是不可能的。在 Swift 世界中,我添加了一个带有按钮的工具栏,用于关闭键盘或执行其他一些功能。
我什至会用顶部的按钮构建一个 ZStack 视图,但我找不到将数字键盘添加到我自己的视图的方法。
在这种情况下,我真正想做的就是在输入数据时关闭数字键盘。我首先尝试修改 SceneDelegate 以关闭点击,但只有当我点击另一个文本或文本字段视图而不是在视图的开放空间中时才有效。
window.rootViewController = UIHostingController(rootView: contentView.onTapGesture {
window.endEditing(true)
})
Run Code Online (Sandbox Code Playgroud)
理想情况下,我会在左下角添加一个 Done 键。如果可以在 SwiftUI 中完成,最好添加一个工具栏。
任何指导将不胜感激。
Xcode 版本 11.2.1 (11B500)
小智 24
使用 UIRepresentable 协议解决了这个问题
struct TestTextfield: UIViewRepresentable {
@Binding var text: String
var keyType: UIKeyboardType
func makeUIView(context: Context) -> UITextField {
let textfield = UITextField()
textfield.keyboardType = keyType
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: textfield.frame.size.width, height: 44))
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(textfield.doneButtonTapped(button:)))
toolBar.items = [doneButton]
toolBar.setItems([doneButton], animated: true)
textfield.inputAccessoryView = toolBar
return textfield
}
func updateUIView(_ uiView: UITextField, context: Context) {
uiView.text = text
}
}
extension UITextField{
@objc func doneButtonTapped(button:UIBarButtonItem) -> Void {
self.resignFirstResponder()
}
}
Run Code Online (Sandbox Code Playgroud)
在内容视图中使用
struct ContentView : View {
@State var text = ""
var body: some View {
TestTextfield(text: $text, keyType: UIKeyboardType.phonePad)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 50)
.overlay(
RoundedRectangle(cornerRadius: 16)
.stroke(Color.blue, lineWidth: 4)
)
}
}
Run Code Online (Sandbox Code Playgroud)
paw*_*222 15
从 iOS 15 开始,我们可以使用新的 ToolbarItemPlacement 在键盘正上方添加自定义视图keyboard:
Form {
...
}
.toolbar {
ToolbarItem(placement: .keyboard) {
Button("Do something") {
print("something")
}
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以将其与新的@FocusState属性包装器结合使用,以从当前编辑的元素中删除焦点:
@FocusState private var isFocused: Bool
...
var body: some View {
Form {
TextField(...)
.focused($isFocused)
}
.toolbar {
ToolbarItem(placement: .keyboard) {
Button("Done") {
isFocused = false
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 14
我使用 SwiftUI-Introspect 库在 5 行中完成了它!我有一个问题,可表示文本字段对填充和框架没有任何反应。一切都是使用这个库决定的!
链接:https : //github.com/siteline/SwiftUI-Introspect
import SwiftUI
import Introspect
struct ContentView : View {
@State var text = ""
var body: some View {
TextField("placeHolder", text: $text)
.keyboardType(.default)
.introspectTextField { (textField) in
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: textField.frame.size.width, height: 44))
let flexButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(textField.doneButtonTapped(button:)))
doneButton.tintColor = .systemPink
toolBar.items = [flexButton, doneButton]
toolBar.setItems([flexButton, doneButton], animated: true)
textField.inputAccessoryView = toolBar
}
}
extension UITextField {
@objc func doneButtonTapped(button:UIBarButtonItem) -> Void {
self.resignFirstResponder()
}
}
Run Code Online (Sandbox Code Playgroud)
If there is an issue with @Binding implement the Coordinator class which conforms to the UITextFieldDelegate. This will allow one to be able to customize the TextField further if required.
struct DecimalTextField: UIViewRepresentable {
private var placeholder: String
@Binding var text: String
init(_ placeholder: String, text: Binding<String>) {
self.placeholder = placeholder
self._text = text
}
func makeUIView(context: Context) -> UITextField {
let textfield = UITextField()
textfield.keyboardType = .decimalPad
textfield.delegate = context.coordinator
textfield.placeholder = placeholder
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: textfield.frame.size.width, height: 44))
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(textfield.doneButtonTapped(button:)))
toolBar.items = [doneButton]
toolBar.setItems([doneButton], animated: true)
textfield.inputAccessoryView = toolBar
return textfield
}
func updateUIView(_ uiView: UITextField, context: Context) {
uiView.text = text
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UITextFieldDelegate {
var parent: DecimalTextField
init(_ textField: DecimalTextField) {
self.parent = textField
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let currentValue = textField.text as NSString? {
let proposedValue = currentValue.replacingCharacters(in: range, with: string) as String
self.parent.text = proposedValue
}
return true
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7737 次 |
| 最近记录: |