diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIKitSearchBar.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIKitSearchBar.swift index 75f0d99c..aa57bdf5 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIKitSearchBar.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIKitSearchBar.swift @@ -7,7 +7,7 @@ struct UIKitSearchBar : UIViewRepresentable { private var placeholder: String - private var focusOnStart = false + @State private var focusOnStart = false private var returnKeyType: UIReturnKeyType = .search @@ -25,7 +25,7 @@ struct UIKitSearchBar : UIViewRepresentable { _text = text self.placeholder = placeholder - self.focusOnStart = focusOnStart + self._focusOnStart = State(initialValue: focusOnStart) self.returnKeyType = returnKeyType self.hideKeyboardOnReturnKeyPress = hideKeyboardOnReturnKeyPress @@ -58,6 +58,14 @@ struct UIKitSearchBar : UIViewRepresentable { func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext) { uiView.text = text + + if focusOnStart { + // on iOS 14 calling .focus() in makeUIView() doesn't work -> do it here and reset focusOnStart property + DispatchQueue.main.async { + focusOnStart = false + } + uiView.focus() + } } func makeCoordinator() -> Cordinator { diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIKitTextField.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIKitTextField.swift index 77c4c57d..a598738f 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIKitTextField.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIKitTextField.swift @@ -16,7 +16,7 @@ struct UIKitTextField: UIViewRepresentable { private var isPasswordField: Bool = false - private var focusOnStart = false + @State private var focusOnStart = false private var focusNextTextFieldOnReturnKeyPress = false @Binding private var focusTextField: Bool @@ -45,7 +45,7 @@ struct UIKitTextField: UIViewRepresentable { self.isPasswordField = isPasswordField - self.focusOnStart = focusOnStart + self._focusOnStart = State(initialValue: focusOnStart) self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress self._focusTextField = focusTextField self.isFocusedChanged = isFocusedChanged @@ -82,10 +82,6 @@ struct UIKitTextField: UIViewRepresentable { Self.NextTagId = Self.NextTagId + 1 // unbelievable, there's no ++ operator textField.tag = Self.NextTagId - if focusOnStart { - textField.focus() - } - textField.textAlignment = textAlignment return textField @@ -93,7 +89,15 @@ struct UIKitTextField: UIViewRepresentable { func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext) { uiView.text = text - + + if focusOnStart { + // on iOS 14 calling .focus() in makeUIView() doesn't work -> do it here and reset focusOnStart property + DispatchQueue.main.async { + focusOnStart = false + } + uiView.focus() + } + if focusTextField { DispatchQueue.main.async { // in very few cases focusTextField gets called during view update resulting in 'undefined behavior' -> async() fixes this uiView.focus()