From cd3a24a3603af01c309cacf527dad67ab58bfa9f Mon Sep 17 00:00:00 2001 From: dankito Date: Mon, 10 Aug 2020 15:55:11 +0200 Subject: [PATCH] Fixed that keyboard covers lower part of the views and that there's been no way so scroll to the end to view --- .../BankingiOSApp.xcodeproj/project.pbxproj | 4 ++ .../BankingiOSApp/ui/AdaptsToKeyboard.swift | 39 +++++++++++++++++++ .../BankingiOSApp/ui/ViewExtensions.swift | 4 ++ .../ui/views/AddAccountDialog.swift | 1 + .../ui/views/BankAccountSettingsDialog.swift | 1 + .../ui/views/BankSettingsDialog.swift | 1 + .../ui/views/EnterTanDialog.swift | 1 + .../ui/views/SelectBankDialog.swift | 1 + .../ui/views/TransferMoneyDialog.swift | 1 + 9 files changed, 53 insertions(+) create mode 100644 ui/BankingiOSApp/BankingiOSApp/ui/AdaptsToKeyboard.swift diff --git a/ui/BankingiOSApp/BankingiOSApp.xcodeproj/project.pbxproj b/ui/BankingiOSApp/BankingiOSApp.xcodeproj/project.pbxproj index a1c94b67..1fd1f5c3 100644 --- a/ui/BankingiOSApp/BankingiOSApp.xcodeproj/project.pbxproj +++ b/ui/BankingiOSApp/BankingiOSApp.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 3607829924E148D40098FEFE /* AdaptsToKeyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3607829824E148D40098FEFE /* AdaptsToKeyboard.swift */; }; 366FA4DA24C472A90094F009 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366FA4D924C472A90094F009 /* Extensions.swift */; }; 366FA4DC24C479120094F009 /* BankInfoListItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366FA4DB24C479120094F009 /* BankInfoListItem.swift */; }; 366FA4E024C4924A0094F009 /* RemitteeListItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366FA4DF24C4924A0094F009 /* RemitteeListItem.swift */; }; @@ -124,6 +125,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 3607829824E148D40098FEFE /* AdaptsToKeyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdaptsToKeyboard.swift; sourceTree = ""; }; 366FA4D924C472A90094F009 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = ""; }; 366FA4DB24C479120094F009 /* BankInfoListItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BankInfoListItem.swift; sourceTree = ""; }; 366FA4DF24C4924A0094F009 /* RemitteeListItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemitteeListItem.swift; sourceTree = ""; }; @@ -400,6 +402,7 @@ 36BE06C724D0DE7400CBBB68 /* UIKitTextField.swift */, 36E21EDA24DC990300649DC8 /* LabelledUIKitTextField.swift */, 36E21EDC24DCA89100649DC8 /* TanProcedurePicker.swift */, + 3607829824E148D40098FEFE /* AdaptsToKeyboard.swift */, ); path = ui; sourceTree = ""; @@ -645,6 +648,7 @@ 36E21ED124DC540400649DC8 /* SettingsDialog.swift in Sources */, 366FA4DC24C479120094F009 /* BankInfoListItem.swift in Sources */, 36FC929E24B39A05002B12E9 /* SceneDelegate.swift in Sources */, + 3607829924E148D40098FEFE /* AdaptsToKeyboard.swift in Sources */, 36E21ECF24DA0EEE00649DC8 /* IconView.swift in Sources */, 36BCF88524C098C8005BEC29 /* BankAccountListItem.swift in Sources */, 36FC92EF24B3BB81002B12E9 /* AddAccountDialog.swift in Sources */, diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/AdaptsToKeyboard.swift b/ui/BankingiOSApp/BankingiOSApp/ui/AdaptsToKeyboard.swift new file mode 100644 index 00000000..b2c9d180 --- /dev/null +++ b/ui/BankingiOSApp/BankingiOSApp/ui/AdaptsToKeyboard.swift @@ -0,0 +1,39 @@ +import SwiftUI +import Combine + + +/** + By default the keyboard covers the lower part of views - and there's not way to scroll down to the end of the dialog. + + Found a solution to fix this bug by : https://stackoverflow.com/a/60178361/8837882. + */ +struct AdaptsToKeyboard: ViewModifier { + + @State var currentHeight: CGFloat = 0 + + + func body(content: Content) -> some View { + GeometryReader { geometry in + content + .padding(.bottom, self.currentHeight) + .animation(.easeOut(duration: 0.16)) + .onAppear(perform: { + NotificationCenter.Publisher(center: NotificationCenter.default, name: UIResponder.keyboardWillShowNotification) + .merge(with: NotificationCenter.Publisher(center: NotificationCenter.default, name: UIResponder.keyboardWillChangeFrameNotification)) + .compactMap { notification in + notification.userInfo?["UIKeyboardFrameEndUserInfoKey"] as? CGRect + } + .map { rect in + rect.height - geometry.safeAreaInsets.bottom + } + .subscribe(Subscribers.Assign(object: self, keyPath: \.currentHeight)) + + NotificationCenter.Publisher(center: NotificationCenter.default, name: UIResponder.keyboardWillHideNotification) + .compactMap { notification in + CGFloat.zero + } + .subscribe(Subscribers.Assign(object: self, keyPath: \.currentHeight)) + }) + } + } +} diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/ViewExtensions.swift b/ui/BankingiOSApp/BankingiOSApp/ui/ViewExtensions.swift index b2d690fa..176b1819 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/ViewExtensions.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/ViewExtensions.swift @@ -4,6 +4,10 @@ import BankingUiSwift extension View { + func fixKeyboardCoversLowerPart() -> some View { + return self.modifier(AdaptsToKeyboard()) + } + func hideNavigationBar() -> some View { return self .navigationBarHidden(true) diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/AddAccountDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/AddAccountDialog.swift index be647d0d..b866f501 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/AddAccountDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/AddAccountDialog.swift @@ -58,6 +58,7 @@ struct AddAccountDialog: View { .alert(item: $errorMessage) { message in Alert(title: message.title, message: message.message, dismissButton: message.primaryButton) } + .fixKeyboardCoversLowerPart() .showNavigationBarTitle("Add account") } diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/BankAccountSettingsDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/BankAccountSettingsDialog.swift index a6266acb..d66d73e2 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/BankAccountSettingsDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/BankAccountSettingsDialog.swift @@ -63,6 +63,7 @@ struct BankAccountSettingsDialog: View { .alert(item: $unsavedChangesMessage) { message in Alert(title: message.title, message: message.message, primaryButton: message.primaryButton, secondaryButton: message.secondaryButton!) } + .fixKeyboardCoversLowerPart() .showNavigationBarTitle(LocalizedStringKey(account.displayName)) .setCancelAndDoneNavigationBarButtons(onCancelPressed: cancelPressed, onDonePressed: donePressed) } diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/BankSettingsDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/BankSettingsDialog.swift index 27b4519a..2e1456e5 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/BankSettingsDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/BankSettingsDialog.swift @@ -95,6 +95,7 @@ struct BankSettingsDialog: View { .alert(item: $askToDeleteAccountMessage) { message in Alert(title: message.title, message: message.message, primaryButton: message.primaryButton, secondaryButton: message.secondaryButton!) } + .fixKeyboardCoversLowerPart() .showNavigationBarTitle(LocalizedStringKey(bank.displayName)) .setCancelAndDoneNavigationBarButtons(onCancelPressed: cancelPressed, onDonePressed: donePressed) } diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift index b66b6c63..3a0545e8 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/EnterTanDialog.swift @@ -128,6 +128,7 @@ struct EnterTanDialog: View { .alert(item: $errorMessage) { message in Alert(title: message.title, message: message.message, dismissButton: message.primaryButton) } + .fixKeyboardCoversLowerPart() .showNavigationBarTitle("Enter TAN Dialog Title") .customNavigationBarBackButton { self.sendEnterTanResult(EnterTanResult.Companion().userDidNotEnterTan()) diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/SelectBankDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/SelectBankDialog.swift index a8ffad72..5d018ed7 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/SelectBankDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/SelectBankDialog.swift @@ -67,6 +67,7 @@ struct SelectBankDialog: View { .alert(item: $errorMessage) { message in Alert(title: message.title, message: message.message, dismissButton: message.primaryButton) } + .fixKeyboardCoversLowerPart() .showNavigationBarTitle("Select Bank Dialog Title") } diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/TransferMoneyDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/TransferMoneyDialog.swift index 070380c9..9356947e 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/TransferMoneyDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/TransferMoneyDialog.swift @@ -155,6 +155,7 @@ struct TransferMoneyDialog: View { return Alert(title: message.title, message: message.message, dismissButton: message.primaryButton) } } + .fixKeyboardCoversLowerPart() .showNavigationBarTitle("Transfer Money Dialog Title") }