Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // TestSheetKeyboard.swift
- // toushare
- //
- // Created by James Mallison on 27/12/2021.
- //
- import SwiftUI
- struct TestKeyboard: View {
- // @State var showingSheet = false
- @State var str: String = ""
- @State var num: Float = 1.2
- ////
- @FocusState private var focusedField: Field?
- private enum Field: Int, CaseIterable {
- case amount
- case str
- }
- //
- var body: some View {
- VStack {
- Spacer()
- TextField("A text field here", text: $str)
- .focused($focusedField, equals: .str)
- TestTextField(value: $num)
- .focused($focusedField, equals: .amount)
- Text("HELLO WORLD")
- Spacer()
- }
- }
- }
- struct TestKeyboard_Previews: PreviewProvider {
- static var previews: some View {
- TestKeyboard()
- }
- }
- // This is what I took and slightly modified to work with Floats.
- // Maybe not the best way using String(value). There's no Formatter use working here either...
- struct TestTextField: UIViewRepresentable {
- @Binding var value: Float
- var keyType: UIKeyboardType = .decimalPad
- var formatter: Formatter = FloatNumberFormatter()
- 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 = String(value)
- }
- }
- extension UITextField{
- @objc func doneButtonTapped(button:UIBarButtonItem) -> Void {
- self.resignFirstResponder()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement