From 42a23ee9fd2a65077880aa1edc75ed50376eb2bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mik=C3=A1=20Kruschel?= <20423069+mikakruschel@users.noreply.github.com> Date: Fri, 26 Mar 2021 14:04:08 +0100 Subject: [PATCH 1/6] Update KeyboardToolbar.swift --- Sources/KeyboardToolbar/KeyboardToolbar.swift | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/Sources/KeyboardToolbar/KeyboardToolbar.swift b/Sources/KeyboardToolbar/KeyboardToolbar.swift index a258525..43763d0 100644 --- a/Sources/KeyboardToolbar/KeyboardToolbar.swift +++ b/Sources/KeyboardToolbar/KeyboardToolbar.swift @@ -32,27 +32,29 @@ internal struct KeyboardToolbar: View { } func itemGroup(_ items: [KeyboardToolbarItem]) -> some View { - ForEach(items) { item in - Button(action: item.callback, label: { - if (item.image != nil || item.text == nil) { - (item.image ?? Image(systemName: "placeholdertext.fill")) - .resizable() - .aspectRatio(contentMode: .fit) - .foregroundColor(item.color) - .frame(height: style.itemSize) - .frame(minWidth: max(style.itemSize, style.height)) - .frame(height: style.height) - } else { - Text(item.text ?? "") - .font(.system(size: style.itemSize)) - .foregroundColor(item.color) - .frame(height: style.itemSize) - .frame(minWidth: max(style.itemSize, style.height)) - .frame(height: style.height) - } - }) - .contentShape(Rectangle()) - } + ForEach(items, content: itemButton) + } + + func itemButton(_ item: KeyboardToolbarItem) -> some View { + Button(action: item.callback, label: { + if (item.image != nil || item.text == nil) { + (item.image ?? Image(systemName: "placeholdertext.fill")) + .resizable() + .aspectRatio(contentMode: .fit) + .foregroundColor(item.color) + .frame(height: style.itemSize) + .frame(minWidth: max(style.itemSize, style.height)) + .frame(height: style.height) + } else { + Text(item.text ?? "") + .font(.system(size: style.itemSize)) + .foregroundColor(item.color) + .frame(height: style.itemSize) + .frame(minWidth: max(style.itemSize, style.height)) + .frame(height: style.height) + } + }) + .contentShape(Rectangle()) } var leadingItemsView: some View { From 86f21003093691d84d4d60997a0d3c7cfc4f554a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mik=C3=A1=20Kruschel?= <20423069+mikakruschel@users.noreply.github.com> Date: Wed, 21 Apr 2021 14:34:17 +0200 Subject: [PATCH 2/6] Improved tappable area --- Sources/KeyboardToolbar/KeyboardToolbar.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/KeyboardToolbar/KeyboardToolbar.swift b/Sources/KeyboardToolbar/KeyboardToolbar.swift index 43763d0..fb1b068 100644 --- a/Sources/KeyboardToolbar/KeyboardToolbar.swift +++ b/Sources/KeyboardToolbar/KeyboardToolbar.swift @@ -44,14 +44,16 @@ internal struct KeyboardToolbar: View { .foregroundColor(item.color) .frame(height: style.itemSize) .frame(minWidth: max(style.itemSize, style.height)) - .frame(height: style.height) + .padding(.vertical, style.height - style.itemSize) + .contentShape(Rectangle()) } else { Text(item.text ?? "") - .font(.system(size: style.itemSize)) + .font(.system(size: style.itemSize + 4)) .foregroundColor(item.color) .frame(height: style.itemSize) .frame(minWidth: max(style.itemSize, style.height)) - .frame(height: style.height) + .padding(.vertical, style.height - style.itemSize) + .contentShape(Rectangle()) } }) .contentShape(Rectangle()) From bb6cb0bd17cf90746fac46947516e41301fc5add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mik=C3=A1=20Kruschel?= <20423069+mikakruschel@users.noreply.github.com> Date: Tue, 21 Sep 2021 13:08:45 +0200 Subject: [PATCH 3/6] Removed unnecessary exclude --- Package.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index edd4b55..fbeb586 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,7 @@ let package = Package( .target( name: "KeyboardToolbar", dependencies: [], - exclude: ["Tests/ExampleViews"] - ), + exclude: [] + ), ] ) From 9f589387325d75c663ff96264c4009ebd0279d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mik=C3=A1=20Kruschel?= <20423069+mikakruschel@users.noreply.github.com> Date: Wed, 22 Sep 2021 10:51:54 +0200 Subject: [PATCH 4/6] Fix bug with toolbar appearing to high and input field not scrolling into view --- Sources/KeyboardToolbar/ViewExtension.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/KeyboardToolbar/ViewExtension.swift b/Sources/KeyboardToolbar/ViewExtension.swift index ac1e478..c5a9214 100644 --- a/Sources/KeyboardToolbar/ViewExtension.swift +++ b/Sources/KeyboardToolbar/ViewExtension.swift @@ -9,7 +9,7 @@ internal struct AppendKeyboardToolbar: ViewModifier { func body(content: Content) -> some View { ZStack { content - .padding(.bottom, (responder.currentHeight == 0 || items.isEmpty) ? 0 : style.height) + .padding(.bottom, (responder.currentHeight == 0 || items.isEmpty) ? 0 : responder.currentHeight + style.height) VStack(spacing: 0) { Spacer() @@ -23,6 +23,7 @@ internal struct AppendKeyboardToolbar: ViewModifier { .padding(.bottom, responder.currentHeight) } .edgesIgnoringSafeArea(.bottom) + .ignoresSafeArea(.keyboard) .animation(.easeOut(duration: responder.duration)) } From 3758cd026f4b6f30ff85c00e5ad1481f15be525c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mik=C3=A1=20Kruschel?= <20423069+mikakruschel@users.noreply.github.com> Date: Fri, 1 Oct 2021 23:08:16 +0200 Subject: [PATCH 5/6] Replaced ZStack with overlay --- .../KeyboardToolbar/KeyboardResponder.swift | 17 +++---------- Sources/KeyboardToolbar/ViewExtension.swift | 24 +++++++------------ 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/Sources/KeyboardToolbar/KeyboardResponder.swift b/Sources/KeyboardToolbar/KeyboardResponder.swift index 408c6da..a629d16 100644 --- a/Sources/KeyboardToolbar/KeyboardResponder.swift +++ b/Sources/KeyboardToolbar/KeyboardResponder.swift @@ -3,8 +3,7 @@ import SwiftUI internal final class KeyboardResponder: ObservableObject { private var notificationCenter: NotificationCenter - @Published private(set) var currentHeight: CGFloat = 0 - @Published private(set) var duration: Double = 0.25 + @Published private(set) var visible: Bool = false init(center: NotificationCenter = .default) { notificationCenter = center @@ -21,21 +20,11 @@ internal final class KeyboardResponder: ObservableObject { } @objc func keyBoardWillShow(notification: Notification) { - if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue { - currentHeight = keyboardSize.height - } - - if let keyboardDuration = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double) { - duration = keyboardDuration - } + visible = true } @objc func keyBoardWillHide(notification: Notification) { - currentHeight = 0 - - if let keyboardDuration = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double) { - duration = keyboardDuration - } + visible = false } } #endif diff --git a/Sources/KeyboardToolbar/ViewExtension.swift b/Sources/KeyboardToolbar/ViewExtension.swift index c5a9214..9fba24c 100644 --- a/Sources/KeyboardToolbar/ViewExtension.swift +++ b/Sources/KeyboardToolbar/ViewExtension.swift @@ -7,24 +7,18 @@ internal struct AppendKeyboardToolbar: ViewModifier { let style: KeyboardToolbarStyle func body(content: Content) -> some View { - ZStack { - content - .padding(.bottom, (responder.currentHeight == 0 || items.isEmpty) ? 0 : responder.currentHeight + style.height) + content.overlay(toolbarContainer, alignment: .bottom) + } + + var toolbarContainer: some View { + VStack(spacing: 0) { + Spacer() - VStack(spacing: 0) { - Spacer() - - if !items.isEmpty { - toolbar - } + if !items.isEmpty { + toolbar } - .opacity(responder.currentHeight == 0 ? 0 : 1) - .animation(.easeOut(duration: responder.duration)) - .padding(.bottom, responder.currentHeight) } - .edgesIgnoringSafeArea(.bottom) - .ignoresSafeArea(.keyboard) - .animation(.easeOut(duration: responder.duration)) + .opacity(responder.visible ? 1 : 0) } var toolbar: some View { From e3c089d71158a63628a10a12e7ff437b3fefecd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mik=C3=A1=20Kruschel?= <20423069+mikakruschel@users.noreply.github.com> Date: Thu, 27 Jan 2022 00:20:24 +0100 Subject: [PATCH 6/6] Use background color behind keyboard This fixes a bug on iPad if a hardware keyboard is used --- Sources/KeyboardToolbar/ViewExtension.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/KeyboardToolbar/ViewExtension.swift b/Sources/KeyboardToolbar/ViewExtension.swift index 9fba24c..eb7eb48 100644 --- a/Sources/KeyboardToolbar/ViewExtension.swift +++ b/Sources/KeyboardToolbar/ViewExtension.swift @@ -42,6 +42,7 @@ internal struct AppendKeyboardToolbar: ViewModifier { KeyboardToolbar(items: items, style: style) } + .background(style.backgroundColor.ignoresSafeArea(.all, edges: .bottom)) } } }