An alternative implementation of the KeyboardResponder
object using Compose
, as seen here.
final class KeyboardResponder: ObservableObject {
let willChange = PassthroughSubject<CGFloat, Never>()
private(set) var currentHeight: Length = 0 {
willSet {
willChange.send(currentHeight)
}
}
let keyboardWillOpen = NotificationCenter.default
.publisher(for: UIResponder.keyboardWillShowNotification)
.first() // keyboardWillShow notification may be posted repeatedly
.map { $0.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect }
.map { $0.height }
let keyboardWillHide = NotificationCenter.default
.publisher(for: UIResponder.keyboardWillHideNotification)
.map { _ in CGFloat(0) }
func listen() {
_ = Publishers.Merge(keyboardWillOpen, keyboardWillHide)
.subscribe(on: RunLoop.main)
.assign(to: .currentHeight, on: self)
}
init() {
listen()
}
}
An even nicer method is to pack the above as a ViewModifier
(loosely adapted from here):
struct AdaptsToSoftwareKeyboard: ViewModifier {
@State var currentHeight: Length = 0
func body(content: Content) -> some View {
content
.padding(.bottom, currentHeight)
.edgesIgnoringSafeArea(currentHeight == 0 ? Edge.Set() : .bottom)
.onAppear(perform: subscribeToKeyboardEvents)
}
private let keyboardWillOpen = NotificationCenter.default
.publisher(for: UIResponder.keyboardWillShowNotification)
.map { $0.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect }
.map { $0.height }
private let keyboardWillHide = NotificationCenter.default
.publisher(for: UIResponder.keyboardWillHideNotification)
.map { _ in Length.zero }
private func subscribeToKeyboardEvents() {
_ = Publishers.Merge(keyboardWillOpen, keyboardWillHide)
.subscribe(on: RunLoop.main)
.assign(to: .currentHeight, on: self)
}
}
And then it could be used like this:
Group {
........
}.modifier(AdaptsToSoftwareKeyboard())
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…