From 98428959bb4b68c6ee57f418d4ca25c585bc9939 Mon Sep 17 00:00:00 2001 From: dankito Date: Mon, 3 Aug 2020 20:41:26 +0200 Subject: [PATCH] Extracted UIKitExtensions --- .../BankingiOSApp/ui/UIKitExtensions.swift | 14 ++++++++++++++ .../BankingiOSApp/ui/UIKitTextField.swift | 6 +++--- .../BankingiOSApp/ui/views/UIKitSearchBar.swift | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 ui/BankingiOSApp/BankingiOSApp/ui/UIKitExtensions.swift diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitExtensions.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitExtensions.swift new file mode 100644 index 00000000..cedbd2b5 --- /dev/null +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitExtensions.swift @@ -0,0 +1,14 @@ +import SwiftUI + + +extension UIResponder { + + @discardableResult func focus() -> Bool { + return becomeFirstResponder() + } + + @discardableResult func clearFocus() -> Bool { + return resignFirstResponder() + } + +} diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift index 43ccc9c5..891fb48b 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift @@ -51,7 +51,7 @@ struct UIKitTextField: UIViewRepresentable { textField.tag = Self.NextTagId if focusOnStart { - textField.becomeFirstResponder() + textField.focus() } return textField @@ -110,11 +110,11 @@ struct UIKitTextField: UIViewRepresentable { let nextView = textField.superview?.superview?.superview?.viewWithTag(nextViewTag) ?? textField.superview?.superview?.superview?.superview?.superview?.viewWithTag(nextViewTag) // for text fields in Lists (tables) - didHandleReturnKey = nextView?.becomeFirstResponder() ?? false + didHandleReturnKey = nextView?.focus() ?? false } if didHandleReturnKey == false { - textField.resignFirstResponder() // default behaviour + textField.clearFocus() // default behaviour } return didHandleReturnKey diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/UIKitSearchBar.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/UIKitSearchBar.swift index a5d32222..cc4e9b46 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/UIKitSearchBar.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/UIKitSearchBar.swift @@ -36,7 +36,7 @@ struct UIKitSearchBar : UIViewRepresentable { searchBar.searchTextField.delegate = context.coordinator if focusOnStart { - searchBar.becomeFirstResponder() + searchBar.focus() } return searchBar