From f05a551bf1dc998ca2e1c7e914c13b38add9ca4d Mon Sep 17 00:00:00 2001 From: dankito Date: Mon, 10 Aug 2020 00:21:39 +0200 Subject: [PATCH] Implemented that a tap on label focuses text field --- .../ui/LabelledUIKitTextField.swift | 19 ++++++++++++++++++- .../BankingiOSApp/ui/UIKitTextField.swift | 14 ++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/LabelledUIKitTextField.swift b/ui/BankingiOSApp/BankingiOSApp/ui/LabelledUIKitTextField.swift index 819ac372..c47dba2e 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/LabelledUIKitTextField.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/LabelledUIKitTextField.swift @@ -15,6 +15,18 @@ struct LabelledUIKitTextField: View { var focusOnStart = false var focusNextTextFieldOnReturnKeyPress = false + @State var focusTextField: Bool = false + + private var focusTextFieldBinding: Binding { + Binding( + get: {print("Getting focusTextFieldBinding for \(self.focusTextField)") + return self.focusTextField }, + set: { + self.focusTextField = $0 + }) + } + + var isUserInputEnabled: Bool = true var actionOnReturnKeyPress: (() -> Bool)? = nil @@ -25,11 +37,16 @@ struct LabelledUIKitTextField: View { var body: some View { HStack { Text(label) + .onTapGesture { + DispatchQueue.main.async { + self.focusTextField = true + } + } Spacer() UIKitTextField(placeholder, text: $text, keyboardType: keyboardType, isPasswordField: isPasswordField, - focusOnStart: focusOnStart, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, + focusOnStart: focusOnStart, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, focusTextField: focusTextFieldBinding, textAlignment: .right, isUserInputEnabled: isUserInputEnabled, actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged) } diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift index a86ab995..86eb5cdf 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift @@ -15,6 +15,7 @@ struct UIKitTextField: UIViewRepresentable { private var focusOnStart = false private var focusNextTextFieldOnReturnKeyPress = false + @Binding private var focusTextField: Bool private var textAlignment: NSTextAlignment = .natural private var isUserInputEnabled: Bool = true @@ -25,7 +26,7 @@ struct UIKitTextField: UIViewRepresentable { init(_ titleKey: String, text: Binding, keyboardType: UIKeyboardType = .default, isPasswordField: Bool = false, - focusOnStart: Bool = false, focusNextTextFieldOnReturnKeyPress: Bool = false, + focusOnStart: Bool = false, focusNextTextFieldOnReturnKeyPress: Bool = false, focusTextField: Binding = .constant(false), textAlignment: NSTextAlignment = .natural, isUserInputEnabled: Bool = true, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { self.placeholder = titleKey @@ -36,6 +37,7 @@ struct UIKitTextField: UIViewRepresentable { self.focusOnStart = focusOnStart self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress + self._focusTextField = focusTextField self.textAlignment = textAlignment self.isUserInputEnabled = isUserInputEnabled @@ -69,7 +71,15 @@ struct UIKitTextField: UIViewRepresentable { } func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext) { - uiView.text = text + uiView.text = text + + if focusTextField { + uiView.focus() + + DispatchQueue.main.async { + self.focusTextField = false // reset value so that it can be set again (otherwise it may never gets resetted and then updateUIView() requests focus even though already another view got the focus in the meantime) + } + } }