Implemented hiding keyboard on return key press for UIKitSearchBar

This commit is contained in:
dankito 2020-09-09 14:29:01 +02:00
parent 96b6f4a718
commit 033a29492d
5 changed files with 40 additions and 8 deletions

View File

@ -34,6 +34,7 @@
366FA4E024C4924A0094F009 /* RemitteeListItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366FA4DF24C4924A0094F009 /* RemitteeListItem.swift */; }; 366FA4E024C4924A0094F009 /* RemitteeListItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366FA4DF24C4924A0094F009 /* RemitteeListItem.swift */; };
366FA4E224C4ED6C0094F009 /* EnterTanDialog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366FA4E124C4ED6C0094F009 /* EnterTanDialog.swift */; }; 366FA4E224C4ED6C0094F009 /* EnterTanDialog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366FA4E124C4ED6C0094F009 /* EnterTanDialog.swift */; };
366FA4E624C6EBF40094F009 /* EnterTanState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366FA4E524C6EBF40094F009 /* EnterTanState.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 */; }; 36B8A4482503D12100C15359 /* ProtectAppSettingsDialog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36B8A4472503D12100C15359 /* ProtectAppSettingsDialog.swift */; };
36B8A44B2503D1E800C15359 /* BiometricAuthenticationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36B8A44A2503D1E800C15359 /* BiometricAuthenticationService.swift */; }; 36B8A44B2503D1E800C15359 /* BiometricAuthenticationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36B8A44A2503D1E800C15359 /* BiometricAuthenticationService.swift */; };
36B8A44D2503D96D00C15359 /* AuthenticationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36B8A44C2503D96D00C15359 /* AuthenticationService.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 = "<group>"; }; 366FA4DF24C4924A0094F009 /* RemitteeListItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemitteeListItem.swift; sourceTree = "<group>"; };
366FA4E124C4ED6C0094F009 /* EnterTanDialog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnterTanDialog.swift; sourceTree = "<group>"; }; 366FA4E124C4ED6C0094F009 /* EnterTanDialog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnterTanDialog.swift; sourceTree = "<group>"; };
366FA4E524C6EBF40094F009 /* EnterTanState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnterTanState.swift; sourceTree = "<group>"; }; 366FA4E524C6EBF40094F009 /* EnterTanState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnterTanState.swift; sourceTree = "<group>"; };
3684EB8A2508F6F00001139E /* SearchBarWithLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchBarWithLabel.swift; sourceTree = "<group>"; };
36B8A4472503D12100C15359 /* ProtectAppSettingsDialog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProtectAppSettingsDialog.swift; sourceTree = "<group>"; }; 36B8A4472503D12100C15359 /* ProtectAppSettingsDialog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProtectAppSettingsDialog.swift; sourceTree = "<group>"; };
36B8A44A2503D1E800C15359 /* BiometricAuthenticationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BiometricAuthenticationService.swift; sourceTree = "<group>"; }; 36B8A44A2503D1E800C15359 /* BiometricAuthenticationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BiometricAuthenticationService.swift; sourceTree = "<group>"; };
36B8A44C2503D96D00C15359 /* AuthenticationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationService.swift; sourceTree = "<group>"; }; 36B8A44C2503D96D00C15359 /* AuthenticationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationService.swift; sourceTree = "<group>"; };
@ -547,6 +549,7 @@
3642F0092500F5AE005186FE /* Divider.swift */, 3642F0092500F5AE005186FE /* Divider.swift */,
361116A7250562BE00315620 /* FaceIDButton.swift */, 361116A7250562BE00315620 /* FaceIDButton.swift */,
361116A9250562CF00315620 /* TouchIDButton.swift */, 361116A9250562CF00315620 /* TouchIDButton.swift */,
3684EB8A2508F6F00001139E /* SearchBarWithLabel.swift */,
); );
path = views; path = views;
sourceTree = "<group>"; sourceTree = "<group>";
@ -783,6 +786,7 @@
360782C324E49FF70098FEFE /* ValidationLabel.swift in Sources */, 360782C324E49FF70098FEFE /* ValidationLabel.swift in Sources */,
36BE069124CEF52800CBBB68 /* UpdateButton.swift in Sources */, 36BE069124CEF52800CBBB68 /* UpdateButton.swift in Sources */,
36E21ED124DC540400649DC8 /* SettingsDialog.swift in Sources */, 36E21ED124DC540400649DC8 /* SettingsDialog.swift in Sources */,
3684EB8B2508F6F00001139E /* SearchBarWithLabel.swift in Sources */,
366FA4DC24C479120094F009 /* BankInfoListItem.swift in Sources */, 366FA4DC24C479120094F009 /* BankInfoListItem.swift in Sources */,
36B8A44B2503D1E800C15359 /* BiometricAuthenticationService.swift in Sources */, 36B8A44B2503D1E800C15359 /* BiometricAuthenticationService.swift in Sources */,
36FC929E24B39A05002B12E9 /* SceneDelegate.swift in Sources */, 36FC929E24B39A05002B12E9 /* SceneDelegate.swift in Sources */,

View File

@ -9,17 +9,27 @@ struct UIKitSearchBar : UIViewRepresentable {
private var focusOnStart = false private var focusOnStart = false
private var returnKeyType: UIReturnKeyType = .search
private var hideKeyboardOnReturnKeyPress = true
private var actionOnReturnKeyPress: (() -> Bool)? = nil private var actionOnReturnKeyPress: (() -> Bool)? = nil
private var textChanged: ((String) -> Void)? = nil private var textChanged: ((String) -> Void)? = nil
init(text: Binding<String>, placeholder: String = "", focusOnStart: Bool = false, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { init(text: Binding<String>, placeholder: String = "", focusOnStart: Bool = false,
returnKeyType: UIReturnKeyType = .search, hideKeyboardOnReturnKeyPress: Bool = true,
actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) {
_text = text _text = text
self.placeholder = placeholder self.placeholder = placeholder
self.focusOnStart = focusOnStart self.focusOnStart = focusOnStart
self.returnKeyType = returnKeyType
self.hideKeyboardOnReturnKeyPress = hideKeyboardOnReturnKeyPress
self.actionOnReturnKeyPress = actionOnReturnKeyPress self.actionOnReturnKeyPress = actionOnReturnKeyPress
self.textChanged = textChanged self.textChanged = textChanged
} }
@ -33,6 +43,9 @@ struct UIKitSearchBar : UIViewRepresentable {
searchBar.searchBarStyle = .minimal searchBar.searchBarStyle = .minimal
searchBar.autocapitalizationType = .none 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.delegate = context.coordinator
searchBar.searchTextField.delegate = context.coordinator searchBar.searchTextField.delegate = context.coordinator
@ -48,7 +61,7 @@ struct UIKitSearchBar : UIViewRepresentable {
} }
func makeCoordinator() -> Cordinator { 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 @Binding var text : String
private var hideKeyboardOnReturnKeyPress: Bool
private var actionOnReturnKeyPress: (() -> Bool)? private var actionOnReturnKeyPress: (() -> Bool)?
private var textChanged: ((String) -> Void)? private var textChanged: ((String) -> Void)?
init(text: Binding<String>, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { init(_ text: Binding<String>, _ hideKeyboardOnReturnKeyPress: Bool, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) {
_text = text _text = text
self.hideKeyboardOnReturnKeyPress = hideKeyboardOnReturnKeyPress
self.actionOnReturnKeyPress = actionOnReturnKeyPress self.actionOnReturnKeyPress = actionOnReturnKeyPress
self.textChanged = textChanged self.textChanged = textChanged
@ -85,7 +102,13 @@ struct UIKitSearchBar : UIViewRepresentable {
func textFieldShouldReturn(_ textField: UITextField) -> Bool { 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
} }

View File

@ -86,7 +86,7 @@ struct AccountTransactionsDialog: View {
VStack { VStack {
Form { Form {
Section { Section {
SearchBarWithLabel(searchTextBinding) { SearchBarWithLabel(searchTextBinding, returnKeyType: .done) {
HStack { HStack {
Text("\(String(self.filteredTransactions.count)) transactions") Text("\(String(self.filteredTransactions.count)) transactions")
.styleAsDetail() .styleAsDetail()

View File

@ -44,7 +44,7 @@ struct SelectBankDialog: View {
var body: some View { var body: some View {
Form { Form {
Section { 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") Text("Search by bank code, bank name or city")
.font(.caption) .font(.caption)
.styleAsDetail() .styleAsDetail()

View File

@ -9,6 +9,8 @@ struct SearchBarWithLabel<Label: View>: View {
private var focusOnStart = false private var focusOnStart = false
private var returnKeyType: UIReturnKeyType = .search
private var actionOnReturnKeyPress: (() -> Bool)? = nil private var actionOnReturnKeyPress: (() -> Bool)? = nil
private var textChanged: ((String) -> Void)? = nil private var textChanged: ((String) -> Void)? = nil
@ -16,7 +18,8 @@ struct SearchBarWithLabel<Label: View>: View {
private let label: () -> Label private let label: () -> Label
init(_ searchText: Binding<String>, placeholder: String = "", focusOnStart: Bool = false, actionOnReturnKeyPress: (() -> Bool)? = nil, init(_ searchText: Binding<String>, placeholder: String = "", focusOnStart: Bool = false,
returnKeyType: UIReturnKeyType = .search, actionOnReturnKeyPress: (() -> Bool)? = nil,
textChanged: ((String) -> Void)? = nil, @ViewBuilder _ label: @escaping () -> Label) { textChanged: ((String) -> Void)? = nil, @ViewBuilder _ label: @escaping () -> Label) {
_searchText = searchText _searchText = searchText
@ -24,6 +27,8 @@ struct SearchBarWithLabel<Label: View>: View {
self.focusOnStart = focusOnStart self.focusOnStart = focusOnStart
self.returnKeyType = returnKeyType
self.actionOnReturnKeyPress = actionOnReturnKeyPress self.actionOnReturnKeyPress = actionOnReturnKeyPress
self.textChanged = textChanged self.textChanged = textChanged
@ -33,7 +38,7 @@ struct SearchBarWithLabel<Label: View>: View {
var body: some View { var body: some View {
VStack { 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() label()
.padding(.horizontal) .padding(.horizontal)