Adjusting view for Keyboard Height on iPad - SwiftUI

When adjusting my view for the Keyboard Height there is additional space between the software keyboard and the view I am trying to move up using the height.

It seems the space is the same space between the sheet and the bottom of the screen.

I have no issues on iPhone, and no issues if the view is part of a NavigationView, but only on iPad and when presented via a .sheet. The size of the space changes on iPad device and if it is in landscape.

The View is a ZStack with a ScrollView and a TextField.

ChatView
Code Block
struct ChatView: View {
@EnvironmentObject var cvm: ChatsModel
@State var addMessage: Bool = false
var body: some View {
ZStack(alignment: .bottomTrailing) {
ScrollView(.vertical, showsIndicators: false) {
LazyVStack {
ForEach(cvm.selectedChat.messages, id: \.uid) { msg in
ChatBubbleView(message: msg)
.id(UUID())
}
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .topLeading)
.padding(16)
}
.zIndex(0)
.onTapGesture {
addMessage = false
}
.ignoresSafeArea(.keyboard, edges: .bottom)
if addMessage == true {
SendMessageView(isActive: self.$addMessage)
.environmentObject(cvm)
.zIndex(1)
.ignoresSafeArea(.keyboard, edges: .bottom)
}
}
.ignoresSafeArea(edges: .bottom)
.ignoresSafeArea(.keyboard, edges: .bottom)
.background(Color("White"))
}
}


SendMessage
Code Block
struct SendMessage: View {
@State var message = ""
@Binding var isActive: Bool
@ObservedObject private var keyboard = KeyboardResponder()
@EnvironmentObject var chatVM: ChatsModel
var body: some View {
VStack(alignment: .leading, spacing: 20) {
HStack(alignment: .center, spacing: 20) {
TextField("message", text: $message)
.frame(height: 38)
.frame(maxWidth: .infinity)
Button(action: {
sendMessage()
}) {
Text("Send")
}
.accentColor(Color("Accent"))
}
.padding(8)
}
.frame(maxWidth: .infinity)
.frame(alignment: .leading)
.KeyboardAwarePadding()
.ignoresSafeArea(edges: .bottom)
}
}

KeyboardAwareModifier
Code Block
struct KeyboardAwareModifier: ViewModifier {
@State private var keyboardHeight: CGFloat = 0
private var keyboardHeightPublisher: AnyPublisher<CGFloat, Never> {
Publishers.Merge(
NotificationCenter.default
.publisher(for: UIResponder.keyboardWillShowNotification)
.compactMap { $0.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue }
.map { $0.cgRectValue.height },
NotificationCenter.default
.publisher(for: UIResponder.keyboardWillHideNotification)
.map { _ in CGFloat(0) }
).eraseToAnyPublisher()
}
func body(content: Content) -> some View {
content
.padding(.bottom, keyboardHeight)
.onReceive(keyboardHeightPublisher) { self.keyboardHeight = $0 }
}
}
extension View {
func KeyboardAwarePadding() -> some View {
ModifiedContent(content: self, modifier: KeyboardAwareModifier())
}
}

Alternative attempt
Code Block
.offset(y: -keyboard.currentHeight)

KeyboardResponder
Code Block
final class KeyboardResponder: ObservableObject {
private var notificationCenter: NotificationCenter
@Published private(set) var currentHeight: CGFloat = 0
init(center: NotificationCenter = .default) {
notificationCenter = center
notificationCenter.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
deinit {
notificationCenter.removeObserver(self)
}
@objc func keyBoardWillShow(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
currentHeight = keyboardSize.height
}
}
@objc func keyBoardWillHide(notification: Notification) {
currentHeight = 0
}
}

Also tried suggestions from other posts, most of which are included in this post: https://stackoverflow.com/questions/57746006/how-to-get-the-keyboard-height-on-multiple-screens-with-swiftui-and-move-the-but

Question with screenshots: https://stackoverflow.com/questions/66309208/adjusting-view-for-keyboard-height-on-ipad-not-working-swiftui



Adjusting view for Keyboard Height on iPad - SwiftUI
 
 
Q