Keyboard offset no longer functions in SwiftUI 2

Viewed 352

So I have an ObservableObject that sets a published variable 'currentHeight' to the height of the keyboard:

import Foundation
import SwiftUI

class KeyboardResponder: ObservableObject {

@Published var currentHeight: CGFloat = 0

var _center: NotificationCenter

init(center: NotificationCenter = .default) {
    _center = center
    _center.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    _center.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}

@objc func keyBoardWillShow(notification: Notification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        withAnimation {
           currentHeight = keyboardSize.height
        }
    }
    print("the KEYBOARD HEIGHT IS \(self.currentHeight)")
}

@objc func keyBoardWillHide(notification: Notification) {
    withAnimation {
       currentHeight = 0
    }
    print("the KEYBOARD HEIGHT IS \(self.currentHeight)")
}
}

In my other views, I create an ObservedObject keyboardResponder and then inside the body of the view, I'll have for example some view where I set the vertical offset:

struct ViewName: View {
    @ObservedObject var keyboardResponder = KeyboardResponder()
    var body: some View {
        GeometryReader { proxy in 
            VStack {
                Text("this should be offset")
                .offset(y: -keyboardResponder.currentHeight)
            }.edgesIgnoringSafeArea(.all)
        }
    }
}

Before Xcode 12/SwiftUI 2 dropped, this worked like a charm but I think they changed something with how views refresh--anyone know what they changed and if there is a solution to my issue here?

EDIT: In my view, if I remove that edgesIgnoringSafeArea(.all), it sort of works, but weirdly. Essentially, the view can only move up to the point where all views are just in frame, it doesn't allow the entire view to shift up (and out of screen view)...I'll throw in a vid to show what I mean...

This is the link for when there are some more elements that take up most of the screen (so the offset is extremely small)

https://youtu.be/jpID11-rZDs

This is the link for when there are fewer elements, so the offset gets that much larger

https://youtu.be/MjqX1qDEA6E

If the GeometryReader is enclosing the view, that's what REALLY breaks it and stops it from responding at all. If I remove it, then it functions as needed...

1 Answers

As of the latest Swift, Xcode 12 and iOS14, I noticed that it is standard build in, when the textfield is not visible when typing. The screen is being risen with the keyboard height and shows the original field. Try this out with a scrollable view and 20 textfields for example. Maybe you can get rid of your observableheight and do it without hard coding it.

enter image description here

Related