From ebf4488e90b3a2c7e7cdc2d7dcb4c400b9018586 Mon Sep 17 00:00:00 2001 From: dankito Date: Wed, 29 Jul 2020 14:14:54 +0200 Subject: [PATCH] Implemented focusing text field right on start and focus() and clearFocus() --- .../BankingiOSApp/ui/UIKitTextField.swift | 33 ++++++++++++++++--- .../ui/views/TransferMoneyDialog.swift | 2 +- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift index 43f408e1..57092c3c 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift @@ -13,19 +13,23 @@ struct UIKitTextField: UIViewRepresentable { private var keyboardType: UIKeyboardType = .default private var isPasswordField: Bool = false + private var focusOnStart = false private var focusNextTextFieldOnReturnKeyPress = false private var actionOnReturnKeyPress: (() -> Bool)? = nil + @State private var textField: UITextField? = nil + init(_ titleKey: String, text: Binding, keyboardType: UIKeyboardType = .default, isPasswordField: Bool = false, - focusNextTextFieldOnReturnKeyPress: Bool = false, actionOnReturnKeyPress: (() -> Bool)? = nil) { + focusOnStart: Bool = false, focusNextTextFieldOnReturnKeyPress: Bool = false, actionOnReturnKeyPress: (() -> Bool)? = nil) { self.placeHolder = titleKey _text = text self.keyboardType = keyboardType self.isPasswordField = isPasswordField + self.focusOnStart = focusOnStart self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress self.actionOnReturnKeyPress = actionOnReturnKeyPress @@ -42,18 +46,39 @@ struct UIKitTextField: UIViewRepresentable { textField.delegate = context.coordinator + // set tag on all TextFields to be able to focus next view (= next tag). See Coordinator for more details Self.NextTagId = Self.NextTagId + 1 // unbelievable, there's no ++ operator textField.tag = Self.NextTagId + DispatchQueue.main.async { // to not update state on view updates (and i only need @State as structs cannot be modified) + self.textField = textField + } + + if focusOnStart { + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { // give view some time to show + textField.becomeFirstResponder() + } + } + return textField } + func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext) { + uiView.text = text + } + + func makeCoordinator() -> UIKitTextField.Coordinator { return Coordinator(text: $text, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, actionOnReturnKeyPress: actionOnReturnKeyPress) } - - func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext) { - uiView.text = text + + + func focus() -> Bool { + return textField?.becomeFirstResponder() ?? false + } + + func clearFocus() -> Bool { + return textField?.resignFirstResponder() ?? false } diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/TransferMoneyDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/TransferMoneyDialog.swift index 7fb03b3a..4c2d5970 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/TransferMoneyDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/TransferMoneyDialog.swift @@ -90,7 +90,7 @@ struct TransferMoneyDialog: View { } Section { - UIKitTextField("Remittee Name", text: $remitteeName, focusNextTextFieldOnReturnKeyPress: true, actionOnReturnKeyPress: handleReturnKeyPress) + UIKitTextField("Remittee Name", text: $remitteeName, focusOnStart: true, focusNextTextFieldOnReturnKeyPress: true, actionOnReturnKeyPress: handleReturnKeyPress) .onReceive(Just(remitteeName)) { newValue in self.isValidRemitteeNameEntered = self.remitteeName.isNotBlank }