From 033a29492d4e2395d37010508bb8137a6e42e6cd Mon Sep 17 00:00:00 2001 From: dankito Date: Wed, 9 Sep 2020 14:29:01 +0200 Subject: [PATCH] Implemented hiding keyboard on return key press for UIKitSearchBar --- .../BankingiOSApp.xcodeproj/project.pbxproj | 4 +++ .../ui/UIKit/UIKitSearchBar.swift | 31 ++++++++++++++++--- .../dialogs/AccountTransactionsDialog.swift | 2 +- .../ui/dialogs/SelectBankDialog.swift | 2 +- .../ui/views/SearchBarWithLabel.swift | 9 ++++-- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/ui/BankingiOSApp/BankingiOSApp.xcodeproj/project.pbxproj b/ui/BankingiOSApp/BankingiOSApp.xcodeproj/project.pbxproj index ab53b599..53512726 100644 --- a/ui/BankingiOSApp/BankingiOSApp.xcodeproj/project.pbxproj +++ b/ui/BankingiOSApp/BankingiOSApp.xcodeproj/project.pbxproj @@ -34,6 +34,7 @@ 366FA4E024C4924A0094F009 /* RemitteeListItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366FA4DF24C4924A0094F009 /* RemitteeListItem.swift */; }; 366FA4E224C4ED6C0094F009 /* EnterTanDialog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366FA4E124C4ED6C0094F009 /* EnterTanDialog.swift */; }; 366FA4E624C6EBF40094F009 /* EnterTanState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366FA4E524C6EBF40094F009 /* EnterTanState.swift */; }; + 3684EB8B2508F6F00001139E /* SearchBarWithLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3684EB8A2508F6F00001139E /* SearchBarWithLabel.swift */; }; 36B8A4482503D12100C15359 /* ProtectAppSettingsDialog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36B8A4472503D12100C15359 /* ProtectAppSettingsDialog.swift */; }; 36B8A44B2503D1E800C15359 /* BiometricAuthenticationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36B8A44A2503D1E800C15359 /* BiometricAuthenticationService.swift */; }; 36B8A44D2503D96D00C15359 /* AuthenticationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36B8A44C2503D96D00C15359 /* AuthenticationService.swift */; }; @@ -177,6 +178,7 @@ 366FA4DF24C4924A0094F009 /* RemitteeListItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemitteeListItem.swift; sourceTree = ""; }; 366FA4E124C4ED6C0094F009 /* EnterTanDialog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnterTanDialog.swift; sourceTree = ""; }; 366FA4E524C6EBF40094F009 /* EnterTanState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnterTanState.swift; sourceTree = ""; }; + 3684EB8A2508F6F00001139E /* SearchBarWithLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchBarWithLabel.swift; sourceTree = ""; }; 36B8A4472503D12100C15359 /* ProtectAppSettingsDialog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProtectAppSettingsDialog.swift; sourceTree = ""; }; 36B8A44A2503D1E800C15359 /* BiometricAuthenticationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BiometricAuthenticationService.swift; sourceTree = ""; }; 36B8A44C2503D96D00C15359 /* AuthenticationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationService.swift; sourceTree = ""; }; @@ -547,6 +549,7 @@ 3642F0092500F5AE005186FE /* Divider.swift */, 361116A7250562BE00315620 /* FaceIDButton.swift */, 361116A9250562CF00315620 /* TouchIDButton.swift */, + 3684EB8A2508F6F00001139E /* SearchBarWithLabel.swift */, ); path = views; sourceTree = ""; @@ -783,6 +786,7 @@ 360782C324E49FF70098FEFE /* ValidationLabel.swift in Sources */, 36BE069124CEF52800CBBB68 /* UpdateButton.swift in Sources */, 36E21ED124DC540400649DC8 /* SettingsDialog.swift in Sources */, + 3684EB8B2508F6F00001139E /* SearchBarWithLabel.swift in Sources */, 366FA4DC24C479120094F009 /* BankInfoListItem.swift in Sources */, 36B8A44B2503D1E800C15359 /* BiometricAuthenticationService.swift in Sources */, 36FC929E24B39A05002B12E9 /* SceneDelegate.swift in Sources */, diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIKitSearchBar.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIKitSearchBar.swift index 88f45ea9..75f0d99c 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIKitSearchBar.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKit/UIKitSearchBar.swift @@ -9,17 +9,27 @@ struct UIKitSearchBar : UIViewRepresentable { private var focusOnStart = false + private var returnKeyType: UIReturnKeyType = .search + + private var hideKeyboardOnReturnKeyPress = true + private var actionOnReturnKeyPress: (() -> Bool)? = nil private var textChanged: ((String) -> Void)? = nil - init(text: Binding, placeholder: String = "", focusOnStart: Bool = false, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { + init(text: Binding, placeholder: String = "", focusOnStart: Bool = false, + returnKeyType: UIReturnKeyType = .search, hideKeyboardOnReturnKeyPress: Bool = true, + actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { + _text = text self.placeholder = placeholder self.focusOnStart = focusOnStart + self.returnKeyType = returnKeyType + self.hideKeyboardOnReturnKeyPress = hideKeyboardOnReturnKeyPress + self.actionOnReturnKeyPress = actionOnReturnKeyPress self.textChanged = textChanged } @@ -33,6 +43,9 @@ struct UIKitSearchBar : UIViewRepresentable { searchBar.searchBarStyle = .minimal searchBar.autocapitalizationType = .none + searchBar.returnKeyType = returnKeyType + searchBar.enablesReturnKeyAutomatically = false // so that return key also gets enabled if no text is entered + searchBar.delegate = context.coordinator searchBar.searchTextField.delegate = context.coordinator @@ -48,7 +61,7 @@ struct UIKitSearchBar : UIViewRepresentable { } func makeCoordinator() -> Cordinator { - return Cordinator(text: $text, actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged) + return Cordinator($text, hideKeyboardOnReturnKeyPress, actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged) } @@ -56,14 +69,18 @@ struct UIKitSearchBar : UIViewRepresentable { @Binding var text : String + private var hideKeyboardOnReturnKeyPress: Bool + private var actionOnReturnKeyPress: (() -> Bool)? private var textChanged: ((String) -> Void)? - init(text: Binding, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { + init(_ text: Binding, _ hideKeyboardOnReturnKeyPress: Bool, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { _text = text + self.hideKeyboardOnReturnKeyPress = hideKeyboardOnReturnKeyPress + self.actionOnReturnKeyPress = actionOnReturnKeyPress self.textChanged = textChanged @@ -85,7 +102,13 @@ struct UIKitSearchBar : UIViewRepresentable { func textFieldShouldReturn(_ textField: UITextField) -> Bool { - return actionOnReturnKeyPress?() ?? false + let didHandleReturnKey = actionOnReturnKeyPress?() ?? false + + if hideKeyboardOnReturnKeyPress || (textField.returnKeyType != .search && didHandleReturnKey == false) { + textField.clearFocus() // default behaviour + } + + return didHandleReturnKey } diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/dialogs/AccountTransactionsDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/dialogs/AccountTransactionsDialog.swift index 07c255f0..4b079fd9 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/dialogs/AccountTransactionsDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/dialogs/AccountTransactionsDialog.swift @@ -86,7 +86,7 @@ struct AccountTransactionsDialog: View { VStack { Form { Section { - SearchBarWithLabel(searchTextBinding) { + SearchBarWithLabel(searchTextBinding, returnKeyType: .done) { HStack { Text("\(String(self.filteredTransactions.count)) transactions") .styleAsDetail() diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/dialogs/SelectBankDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/dialogs/SelectBankDialog.swift index e88ec119..0ad71f33 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/dialogs/SelectBankDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/dialogs/SelectBankDialog.swift @@ -44,7 +44,7 @@ struct SelectBankDialog: View { var body: some View { Form { Section { - SearchBarWithLabel(searchTextBinding, placeholder: "Bank code, bank name or city", focusOnStart: true) { + SearchBarWithLabel(searchTextBinding, placeholder: "Bank code, bank name or city", focusOnStart: true, returnKeyType: .done) { Text("Search by bank code, bank name or city") .font(.caption) .styleAsDetail() diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/SearchBarWithLabel.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/SearchBarWithLabel.swift index 9d3f7c44..e4aa890e 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/SearchBarWithLabel.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/SearchBarWithLabel.swift @@ -9,6 +9,8 @@ struct SearchBarWithLabel: View { private var focusOnStart = false + private var returnKeyType: UIReturnKeyType = .search + private var actionOnReturnKeyPress: (() -> Bool)? = nil private var textChanged: ((String) -> Void)? = nil @@ -16,7 +18,8 @@ struct SearchBarWithLabel: View { private let label: () -> Label - init(_ searchText: Binding, placeholder: String = "", focusOnStart: Bool = false, actionOnReturnKeyPress: (() -> Bool)? = nil, + init(_ searchText: Binding, placeholder: String = "", focusOnStart: Bool = false, + returnKeyType: UIReturnKeyType = .search, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil, @ViewBuilder _ label: @escaping () -> Label) { _searchText = searchText @@ -24,6 +27,8 @@ struct SearchBarWithLabel: View { self.focusOnStart = focusOnStart + self.returnKeyType = returnKeyType + self.actionOnReturnKeyPress = actionOnReturnKeyPress self.textChanged = textChanged @@ -33,7 +38,7 @@ struct SearchBarWithLabel: View { var body: some View { VStack { - UIKitSearchBar(text: $searchText, placeholder: placeholder, focusOnStart: focusOnStart, actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged) + UIKitSearchBar(text: $searchText, placeholder: placeholder, focusOnStart: focusOnStart, returnKeyType: returnKeyType, actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged) label() .padding(.horizontal)